Harshavardhan Ramanna
Harshavardhan Ramanna

Reputation: 738

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?

Upvotes: 9

Views: 5687

Answers (2)

terabithia
terabithia

Reputation: 9

I know this thread is super old. But for folks like me who just getting started can use petsy. Checkout the answer discussed here -> how to the remove interaction-only columns from sklearn.preprocessing.PolynomialFeatures

Upvotes: 0

lejlot
lejlot

Reputation: 66825

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)))

Upvotes: 11

Related Questions