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

问题描述 投票:0回答:1

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

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
python machine-learning linear-regression
1个回答
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

您只是在干运行期间考虑第一个样本进行更新,因此不匹配。

© www.soinside.com 2019 - 2024. All rights reserved.