-
Notifications
You must be signed in to change notification settings - Fork 0
/
2..py
42 lines (30 loc) · 849 Bytes
/
2..py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import random
import numpy as np
import matplotlib.pyplot as plt
import random
# with open('data.csv', 'w') as f:
# f.write('')
# for i in range(200):
# with open('data.csv', 'a+') as f:
# x = random.random()*50
# y = random.random()*50
# f.write(f'{x},{y}\n')
# points = np.genfromtxt('data.csv', delimiter=',')
# # print(points[0,0])
# # 提取points中的两列数据分别作为x,y
# x = points[:, 0]
# y = points[:, 1]
# plt.scatter(x, y)
# plt.show()
# 定义损失函数,另外还要有数据x,y
def compute_cost(w, b, points):
total_cost = 0
M = len(points)
for i in range(M):
x = points[i, 0]
y = points[i, 1]
total_cost += (y - w * x - b) ** 2
return total_cost / M
# 定义核心算法拟合函数
def average(data):
return sum(data) / len(data)