Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you remove only the interaction terms in a polynomial regression using scikit-learn?

I am running a polynomial regression using scikit-learn. I have a large number of variables (23 to be precise) which I am trying to regress using polynomial regression with degree 2.

interaction_only = True, keeps only the interaction terms such as X1*Y1, X2*Y2, and so on.

I want only the other terms i.e, X1, X12, Y1, Y12, and so on.

Is there a function to get this?

like image 436
Harshavardhan Ramanna Avatar asked Aug 03 '16 05:08

Harshavardhan Ramanna


People also ask

What does Polynomialfeatures do in Python?

Generate polynomial and interaction features. Generate a new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree.

What is Polynomialfeatures in Sklearn?

Polynomial features are those features created by raising existing features to an exponent. For example, if a dataset had one input feature X, then a polynomial feature would be the addition of a new feature (column) where values were calculated by squaring the values in X, e.g. X^2.


1 Answers

There is no such function, because the transormation can be easily expressed with numpy itself.

X = ... 
new_X = np.hstack((X, X**2))

and analogously if you want to add everything up to degree k

new_X = np.hstack((X**(i+1) for i in range(k)))
like image 200
lejlot Avatar answered Sep 27 '22 10:09

lejlot