我刚开始学习机器学习,目前正修读吴志强的机器学习课程。我已经用python实现了线性回归算法,但结果并不理想。I python代码如下:import numpy as np
x = [[1,1,1,1,1,1,1,1,1,1],[10,20,30,40,50,60,70,80,90,100]]
y = [10,16,20,23,29,30,35,40,45,50]
x = np.array(x)
y = np.array(y)
theta = np.zeros((2,1))
def Cost(x,y,theta):
m = len(y)
pred_ions = np.transpose(theta).dot(x)
J = 1/(2*m) * np.sum((pred_ions - y)*(pred_ions - y))
return J
def GradientDescent(x,y,theta,iteration,alpha):
m = len(y)
pred_ions = np.transpose(theta).dot(x)
i = 1
while i <= iteration:
theta[0] = theta[0] - alpha/m * np.sum(pred_ions - y)
theta[1] = theta[1] - alpha/m * np.sum((pred_ions - y)*x[1,:])
Cost_History = Cost(x,y,theta)
i = i + 1
return theta[0],theta[1]
itera = 1000
alpha = 0.01
a,b = GradientDescent(x,y,theta,itera, alpha)
print(a)
print(b)
我不知道到底是什么问题。但是,我的结果很奇怪。根据上述代码,参数值为298和19890。任何帮助都将不胜感激。谢谢。在