简体   繁体   English

使用梯度下降实现线性回归

[英]Implementing Linear Regression using Gradient Descent

I have just started in machine learning and currently taking the course by andrew Ng's Machine learning Course.我刚刚开始学习机器学习,目前正在学习 Andrew Ng 的机器学习课程。 I have implemented the linear regression algorithm in python but the result is not desirable.我已经在 python 中实现了线性回归算法,但结果并不理想。 I code of python is as follows:我的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)

I am not able to figure out what exactly is the problem.我无法弄清楚到底是什么问题。 But, my results are something very strange.但是,我的结果很奇怪。 The value of parameter is, according to above code, are 298 and 19890. Any Help would be appreciated.根据上面的代码,参数的值是 298 和 19890。任何帮助将不胜感激。 Thanks.谢谢。

Ah.啊。 I did this assignment too a while ago.我前段时间也做过这个任务。

See this mentioned in Page 7 of the assignment PDF:请参阅作业 PDF 的第 7 页中提到的这一点:

Octave/MATLAB array indices start from one, not zero. Octave/MATLAB 数组索引从 1 开始,而不是从 0 开始。 If you're storing θ0 and θ1 in a vector called theta, the values will be theta(1) and theta(2).如果将 θ0 和 θ1 存储在名为 theta 的向量中,则值将是 theta(1) 和 theta(2)。

So, in your while loop, change the theta[0] and theta[1] to theta[1] and theta[2] .因此,在您的 while 循环中,将theta[0]theta[1]更改为theta[1]theta[2] It should work right.它应该可以正常工作。

Also, if you are storing the Cost in Cost_History, shouldn't it include the iteration variable like此外,如果您将成本存储在 Cost_History 中,它不应该包含迭代变量,如

Cost_History[i] = Cost(x,y,theta)

Just check that too!也只是检查一下! Hope this helped.希望这有帮助。

Edit 1: Okay, I have understood the issue now.编辑 1:好的,我现在已经理解了这个问题。 In his video, Andrew Ng says that you need to update both the thetas simultaneously.在他的视频中,Andrew Ng 说你需要同时更新两个 theta To do that, store the theta matrix in a temp variable.为此,请将 theta 矩阵存储在临时变量中。 And update theta[0] and theta[1] based on the temp values.并根据温度值更新 theta[0] 和 theta[1]。

Currently in your code, during theta[1] = it has changed the theta[0] to the newer value already, so both are not being updated simultaneously.当前在您的代码中,在theta[1] =它已经将theta[0]更改为较新的值,因此两者不会同时更新。

So instead, do this:因此,请执行以下操作:

while i <= iteration:
    temp = theta
    theta[0] = theta[0] - alpha/m * np.sum(np.transpose(temp).dot(x) - y)
    theta[1] = theta[1] - alpha/m * np.sum((np.transpose(temp).dot(x) - y)*x[1,:])
    Cost_History[i] = Cost(x,y,theta)
    i = i + 1

It should work now, if not, let me know, I will debug on my side.现在应该可以了,如果不行,请告诉我,我会在我这边调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM