当运行python多元回归,进行预测时报错:ValueError: Expected 2D array, got 1D array instead:
代码:
from numpy import genfromtxt
from sklearn import linear_model
datapath=r"Delivery_Dummy.csv"
data = genfromtxt(datapath,delimiter=",",encoding='utf-8')
x = data[1:,:-1]
y = data[1:,-1]
print(x)
print(y)
mlr = linear_model.LinearRegression()
mlr.fit(x, y)
print(mlr)
print("coef:")
print(mlr.coef_)
print("intercept")
print(mlr.intercept_)
xPredict = [90,2,0,0,1]
yPredict = mlr.predict(xPredict)
print("predict:")
print(yPredict)
定位到是:
yPredict = mlr.predict(xPredict)
预测这一行代码报错,意思是需要2维数据,却给了一个1维的,再数据价格中括号即可:
xPredict = [[90,2,0,0,1]]
yPredict = mlr.predict(xPredict)