import numpy as np
import matplotlib.pyplot as plt
points = np.genfromtxt("data.csv",delimiter=",")
#points
#提取points中的兩列數據,分別做爲x,y
x=points[:,0];
y=points[:,1];
#用plt畫出散點圖
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 #一除都是浮點 兩個除號是地板除,整型。 如 3 // 4
# 先定義一個求均值的函數 問題 求均值是否是能夠直接用np.mean(data)來實現?
# def average(data):
# sum=0
# num=len(data)
# for i in range(num):
# sum += data[i]
# return sum/num
# print(average(x))
# print(np.mean(x))
#打印出來結果同樣,能夠通用。
#定義核心擬合函數
def fit(points):
M = len(points)
x_bar=np.mean(points[:,0])
sum_yx= 0
sum_x2=0
sum_delta =0
for i in range(M):
x=points[i,0]
y=points[i,1]
sum_yx += y*(x-x_bar)
sum_x2 += x**2
#根據公式計算w
w = sum_yx/(sum_x2-M*(x_bar**2))
for i in range(M):
x=points[i,0]
y=points[i,1]
sum_delta += (y-w*x)
b = sum_delta / M
return w,b
w,b =fit(points)
w,b
print ("w is :",w)
print ("b is :",b)
cost = compute_cost(w,b,points)
print("cost is :" ,cost)
plt.scatter(x,y)
pred_y= w*x+b
plt.plot(x,pred_y,c='r')