sklearn 回归器: ValueError: 发现样本数量不一致的数组



我正在尝试使用 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:所说:

在 0.17 中不推荐使用将 1d 数组作为数据传递,并将提高 0.19 中的值错误。使用 X.reshape(-1, 1( 重塑数据,如果 您的数据具有单个要素或 X.reshape(1, -1( 如果它包含 单个样本。

所以试试这个:

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)

最新更新