我是Python线性回归概念的新手。我在scikit-learn中使用线性回归来找到y的预测值,这里称为y_new。下面的代码是我到目前为止编写的代码:
import numpy as np
#creating data for the run
x=spendings = np.linspace(0,5,4000)
y=sales = np.linspace(0,0.5,4000)
#defining the training function
def train(x,y):
from sklearn.linear_model import LinearRegression
model = LinearRegression().fit(x,y)
return model
model = train(x,y)
x_new = 23.0
y_new = model.predict([[x_new]])
print(y_new)
由于此错误消息,我无法获取y_new的值:
Expected 2D array, got 1D array instead:
array=[0.00000000e+00 1.25031258e-03 2.50062516e-03 ... 4.99749937e+00
4.99874969e+00 5.00000000e+00].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
根据LinearRegression fit method的文档,期望X和y输入为((n_samples,n_features)形状。
如果您检查x和y形状,就是这样
x=spendings = np.linspace(0,5,4000)
y=sales = np.linspace(0,0.5,4000)
print(x.shape)
print(y.shape)
(4000,)
(4000,)
什么错误说,您需要使用arr.reshape(-1,1)
重塑x和y的形状以形成((n_samples,n_features)]。因此,您需要先将[x]和[y]设置为reshape,然后再拟合到LinearRegression。
x = x.reshape(-1,1)
y = y.reshape(-1,1)
print(x.shape)
print(y.shape)
(4000, 1)
(4000, 1)