【问题标题】:PolynomialFeatures fit_transform is giving Value errorPolynomialFeatures fit_transform 给出值错误
【发布时间】:2014-12-14 22:24:33
【问题描述】:

我在尝试运行多项式回归示例时遇到 ValueError:

from sklearn.preprocessing import PolynomialFeatures
import numpy as np

poly = PolynomialFeatures(degree=2)
poly.fit_transform(X)   ==> ERROR

错误是:

File "/root/.local/lib/python2.7/site-packages/sklearn/base.py", line 426, in fit_transform
    return self.fit(X, **fit_params).transform(X)

File "/root/.local/lib/python2.7/site-packages/sklearn/preprocessing/data.py", line 473, in fit
  self.include_bias)

File "/root/.local/lib/python2.7/site-packages/sklearn/preprocessing/data.py", line 463, in _power_matrix
  powers = np.vstack(np.bincount(c, minlength=n_features) for c in combn)

File "/usr/lib/python2.7/dist-packages/numpy/core/shape_base.py", line 226, in vstack
  return _nx.concatenate(map(atleast_2d,tup),0)

File "/root/.local/lib/python2.7/site-packages/sklearn/preprocessing/data.py", line 463, in <genexpr>

  powers = np.vstack(np.bincount(c, minlength=n_features) for c in combn)  
  ValueError: The first argument cannot be empty.

我的 scikit-learn 版本是 0.15.2

此示例来自:http://scikit-learn.org/stable/modules/linear_model.html#polynomial-regression-extending-linear-models-with-basis-functions

【问题讨论】:

  • >>> X.shape (3, 2) 即使在这个例子中也会发生这种情况:scikit-learn.org/stable/modules/…
  • 您能告诉我您使用的是哪个 NumPy 版本吗?我无法在本地重现此内容。

标签: python numpy scikit-learn scipy


【解决方案1】:

在创建这样的 PolynomialFeatures 类的对象时,您应该尝试将 include_bias 设置为 False

poly = PolynomialFeatures(degree=2, include_bias=False)

请注意,示例中的最终矩阵现在没有第一列。

【讨论】:

    猜你喜欢
    • 2018-05-20
    • 2021-09-13
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 2019-11-11
    相关资源
    最近更新 更多