Data must be 1-dimensional为什么报错啊?第28行开始这个错

来源:3-4 回归分析实战

慕粉8371238

2019-07-28 19:30

from numpy import *
from numpy.linalg import inv
import operator
import pandas as pd

dataset = pd.read_csv('data.csv')
temp=dataset.iloc[:,0:3]
Y=dataset.iloc[:,3]
X=temp
X['b']=1
#print (Y)

##最小二乘
#theta=inv(X.T*X)*X.T*Y
theta=dot(dot(inv(dot(X.T,X)),X.T),Y)
print(theta)

#梯度下降
alpha=0.01
theta1=array([1., 1., 1., 1.]).reshape(4, 1)
X1=X.iloc[:, 0]
X2=X.iloc[:, 1]
X3=X.iloc[:, 2]
B=X.iloc[:, 3]
t=theta1
print(X2)
for i in range(10000):
    t[0] = theta1[0] - alpha * sum((dot(X, theta1)-Y)*X1)/200.
    t[1] = theta1[1] - alpha * sum((dot(X, theta1) - Y) * X2) / 200.
    t[2] = theta1[2] - alpha * sum((dot(X, theta1) - Y) * X3) / 200.
    t[3] = theta1[3] - alpha * sum((dot(X, theta1) - Y) * B) / 200.
    theta1 = t


写回答 关注

1回答

  • 慕粉_3537449
    2019-12-27 16:28:44
    已采纳
    dot(X, theta1)返回的是一个 200 * 1 矩阵,而 Y不是一个200 * 1的矩阵,所以需要对Y进行重新规划,
    Y = Y.values.reshape(200, 1)就可以了


Python实现线性回归

掌握python进行线性回归分析的原理及编程实践

26153 学习 · 36 问题

查看课程

相似问题