def compute_SquareCost(w,b,points_set):
total_cost =0
this_cost =0print(f'我进来了',end='\n')
#开始准点计算损失
for i in range(len(points_set)):
x = points_set[i,0]
y = points_set[i,1]
this_cost =(y-(w*x+b))**2print(f'本次的损失函数值为:{this_cost}',end='\n')
total_cost+=this_cost
return total_cost/len(points_set)
4.使用sklearn库进行计算
lr = slm.LinearRegression()
x_newSquare = x_set.reshape(-1,1)
y_newSquare = y_set.reshape(-1,1)
lr.fit(x_newSquare,y_newSquare)
5.提取出相关系数,进行验证
w = lr.coef_[0][0]
b = lr.intercept_[0]print("w is: ", w)print("b is: ", b)
cost =compute_SquareCost(w,b,points_set)print("cost is: ", cost)