0
$\begingroup$

I got confused when I used 10 degrees and got 11 outputs. I checked the https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.PolynomialFeatures.html and there seems to be (1) column added to PolynomialFeatures.

For instance, in the example that is provided on that page:

import numpy as np 
from sklearn.preprocessing import PolynomialFeatures 
X = np.arange(6).reshape(3, 2) 
X
array([[0, 1],
   [2, 3],
   [4, 5]])
poly = PolynomialFeatures(2) 
poly.fit_transform(X)

Returns

array([[ 1.,  0.,  1.,  0.,  0.,  1.],
   [ 1.,  2.,  3.,  4.,  6.,  9.],
   [ 1.,  4.,  5., 16., 20., 25.]])

What is the purpose of "1." elements at the beginning of each list?

$\endgroup$

1 Answer 1

1
$\begingroup$

The first term is one of the input column to the power of zero. It works as an intercept in a regression model.

Check this question and answers for details: When is it ok to remove the intercept in a linear regression model?

$\endgroup$
6
  • $\begingroup$ I see, then any regression further has to have fit_intercept=False enabled. If set to True, it returns 0s for intercept. $\endgroup$
    – folderj
    Commented Jan 10, 2021 at 19:49
  • $\begingroup$ Did you already include a column of ones? check also this Q/A: stackoverflow.com/questions/46779605/… $\endgroup$
    – Ale
    Commented Jan 10, 2021 at 20:13
  • $\begingroup$ Yes, after I run PolynomialFeatures(2).fit_transform(x_train) on my train set, and then use Lasso with fit_intercept=False or fit_intercept=True, I get 3 outputs, I get zeros as first element of three in the latter case (set to True):[1.71 1.81 0.61] and [0. 1.91 0.68]. I'm bootstrapping, so I'm getting many results, but 0s are consistent. $\endgroup$
    – folderj
    Commented Jan 10, 2021 at 20:23
  • $\begingroup$ This is different from the answer in your link. So it's not clear what happens when it's set to True, but I guess it never should be set to True. $\endgroup$
    – folderj
    Commented Jan 10, 2021 at 20:30
  • $\begingroup$ Sorry but I don't know the details behind your comment so I cannot help you on that. It would need another question. There is no need to include another column of ones if you use PolynomialFeatures(), since it already does it. $\endgroup$
    – Ale
    Commented Jan 10, 2021 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.