读取数据+特征归一化
mean是均值
将特征大小控制在 -1~1之间
房屋的面积对价格的影响
卧室数量对价格的影响
损失函数
def costFunction(X,y,theta):
inner =np.power(X@theta-y,2)
return np.sum(inner)/(2*len(X))
梯度下降
def gradientDescent(X,y,theta,alpha,iters):
costs = []
for i in range(iters):
theta = theta - (X.T @ (X@theta - y) ) * alpha / len(X)
cost = costFunction(X,y,theta)
costs.append(cost)
if i % 100 == 0:
print(cost)
return theta,costs