python - sklearn回归器: ValueError: Found arrays with inconsistent numbers of samples

标签 python scikit-learn

我正在尝试使用 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/

相关文章:

python - 执行网格搜索后分配模型参数

cluster-analysis - 小批量 k 均值返回少于 k 个集群

python - sklearn 错误 - 我已经填写了列的缺失值但仍然面临以下错误

python - 具有先前主题词的潜在狄利克雷分配

python - 如何与 Odoo 9 一起运行 Odoo 8?

python - 如何部署使用 export_saved_model 保存的 TensorFlow 模型

python - __iter__() 作为生成器实现

python "string"模块?

python - Django 模型继承和类型检查

python - 执行 scikit-learn 线性回归模型时出现问题