user3868051
user3868051

Reputation: 1249

how to the remove interaction-only columns from sklearn.preprocessing.PolynomialFeatures

As per the description given below: 'if an input sample is two dimensional and of the form [a, b], the degree-2 polynomial features are [1, a, b, a^2, ab, b^2].'

sklearn.preprocessing.PolynomialFeatures

I need my output ndarray generated to be of the type: [a, b, a^2, b^2] I know that poly = PolynomialFeatures(2, include_bias=False) poly.fit_transform(X) gives the output like : [a, b, a^2, ab, b^2]. But I do not want these intermediate 'ab' type columns that are being generated. How to do that any idea? Or any better API that can be used here?

Upvotes: 2

Views: 2092

Answers (1)

Marcus V.
Marcus V.

Reputation: 6869

Check out patsy and this scikit-learn integration patsylearn.

This gives you full control in a R-like formula:

from patsylearn import PatsyTransformer
transformer = PatsyTransformer("y ~ a + b + a^2 + b^2")
transformer.fit(data)

Upvotes: 2

Related Questions