【问题标题】:sklearn regressor: ValueError: Found arrays with inconsistent numbers of samplessklearn 回归器:ValueError:发现样本数量不一致的数组
【发布时间】:2017-10-24 10:59:15
【问题描述】:

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

【问题讨论】:

    标签: python scikit-learn


    【解决方案1】:

    正如DeprecationWarning: 所说:

    将一维数组作为数据传递在 0.17 中已弃用,并将引发 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)
    

    【讨论】:

      猜你喜欢
      • 2016-05-16
      • 2023-03-15
      • 2016-05-17
      • 2017-12-31
      • 2016-05-24
      • 2016-09-12
      • 2017-03-12
      • 1970-01-01
      • 2015-08-29
      相关资源
      最近更新 更多