简体   繁体   中英

PolynomialFeatures fit_transform is giving Value error

I am getting a ValueError while trying to run the Polynomial Regression example:

from sklearn.preprocessing import PolynomialFeatures
import numpy as np

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

The error is:

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.

My scikit-learn version is 0.15.2

This is example is taken from: http://scikit-learn.org/stable/modules/linear_model.html#polynomial-regression-extending-linear-models-with-basis-functions

You should try to set include_bias to False when creating object of PolynomialFeatures class like this

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

Note that the final matrix in the example does not have the first column now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM