线性回归怀疑。手动验证值

时间:2019-02-17 08:03:43

标签: python machine-learning linear-regression

我有以下代码来测试线性回归。但是,当我运行代码并尝试手动验证值时,没有一个匹配。有人可以告诉我我要去哪里错吗? 代码:

def hypothesis(theta,x):
    return theta[0] + theta[1]*x

def gradient(Y,X,theta):
    grad = np.array([0.0,0.0])
    m = X.shape[0]
    for i in range(m):
        grad[0] += -1*(Y[i] - hypothesis(theta,X[i]))
        grad[1] += -1*(Y[i] - hypothesis(theta,X[i]))*X[i]        
    return grad

def gradientDescent(X,Y,learning_rate,maxItr):
    grad = np.array([0.0,0.0])
    theta = np.array([0.0,0.0])


for i in range(maxItr):
    grad = gradient(Y,X,theta)

    theta[0] = theta[0] - learning_rate*grad[0]
    theta[1] = theta[1] - learning_rate*grad[1]
    print(theta[0],theta[1])

return theta

theta = gradientDescent(X,Y,learning_rate=0.001,maxItr=10)
print(theta[0],theta[1])

选择的样本值:

X values are [8., 9.1, 8.4, 6.9, 7.7]
Y values are [0.99007,0.99769,0.99386,0.99508,0.9963]

对于第一次迭代,theta [0],theta [1]均为零。

grad[0] will be
    grad = gradient(Y,X,theta)
        grad[0] += -1*(Y[i] - hypothesis(theta,X[i]))
        grad[1] += -1*(Y[i] - hypothesis(theta,X[i]))*X[i]
                                hypothesis is = theta[0] + theta[1]*x

因此

 grad[0] = -1* (0.99007 - (0+0*8)) = -0.99007
 grad[1] = -grad[0]*8 (it is x value) = -7.92056

将这些值代入gradientDescent函数

 theta[0] = theta[0] - learning_rate*grad[0]   = 0 - 0.001*0.99007 = 
 0.00099007
 theta[1] = theta[1] - learning_rate*grad[1]   = 0 - 0.001*-7.92056 = .00792056

在下一次迭代中,

Theta[0] = 0.00099007 - learning_rate*grad[0]
theta[1] = .00792056 - learning_rate*grad[1]

代码输出

0.09866678000000005 0.3350843409218396
0.07498296640684816 0.1558123257099002
0.11387616788364091 0.2419456001907594
0.11997964181118528 0.19131764455613343
0.14248899521134195 0.2113445328779582
0.15604123217648141 0.1950485653432334
0.1737269628368541 0.19760149929164242
0.1888040642102288 0.190546128695709
0.20475901586865816 0.18855714748276606
0.21980269083315218 0.1840639014102274

1 个答案:

答案 0 :(得分:0)

在这里,经过一轮迭代,您必须根据代码考虑所有样本的梯度求和。

theta[0] = theta[0] - learning_rate*grad[0]   = 0 - 0.001*0.99007 = 
 0.00099007
 theta[1] = theta[1] - learning_rate*grad[1]   = 0 - 0.001*-7.92056 = .00792056

您在更新时仅考虑空运行期间的第一个样本,因此不匹配。