多个变量的梯度下降可预测不同数据点的相同值

时间:2018-06-24 10:06:19

标签: machine-learning gradient-descent

线性回归-这是我针对多个变量的基本梯度下降代码:

from sklearn import datasets
import numpy as np
from sklearn.model_selection import train_test_split
import pandas as pd
data=pd.DataFrame(datasets.load_diabetes().data,columns=datasets.load_diabetes().feature_names)

#Add a column of ones to X as x0=1 

data=pd.concat([pd.DataFrame(np.ones((data.shape[0],1)),columns=["x0"]),data],axis=1)
x,x_,y,y_=train_test_split(data.values,datasets.load_diabetes().target.ravel(),test_size=0.2)
n=x.shape[1]
itr=2000
lr=0.001
theta=np.zeros((n,1))
for i in range(itr):
    prediction=x.dot(theta)
    for j in range(n):
        tmp=x[0:,j]
        tmp.shape=((tmp.shape[0],1))
        error=(prediction-y)*tmp
        theta[j][0]-=(lr*(1.0/x.shape[0])*error.sum())

#predicting values
print(*x_.dot(theta))

它正在输出[ 152.89519241] [ 152.8987145] [ 152.89582699] ...

我花了2个小时来查找问题,但我失败了,这是我在做什么错了?

0 个答案:

没有答案