我正在尝试使用 sklearn 进行简单的回归,但我不明白如何手动制作自己的数据
import numpy as np
from sklearn import linear_model
Y = np.array([22000, 13400, 47600, 7400, 12000, 32000, 28000, 31000, 69000, 48600])
X = np.array([0.62, 0.24, 0.89, 0.11, 0.18, 0.75, 0.54, 0.61, 0.92, 0.88])
# Create linear regression object
regr = linear_model.LinearRegression()
# Train the model using the training sets
regr.fit(X, Y)
我收到此错误:
ValueError: Found arrays with inconsistent numbers of samples: [ 1 10]
最佳答案
正如DeprecationWarning:
所说:
Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
所以试试这个:
In [70]: regr.fit(X[:, None], Y)
Out[70]: LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
或
In [71]: regr.fit(X.reshape(-1, 1), Y)
Out[71]: LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
关于python - sklearn回归器: ValueError: Found arrays with inconsistent numbers of samples,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44152617/