PolynomialFeatures fit_transform给出了Value错误

时间:2014-10-20 01:01:59

标签: python numpy scikit-learn scipy

我在尝试运行多项式回归示例时遇到了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

1 个答案:

答案 0 :(得分:0)

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

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

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