I know it is possible to obtain the polynomial features as numbers by using: polynomial_features.transform(X)
. According to the manual, for a degree of two the features are: [1, a, b, a^2, ab, b^2]
. But how do I obtain a description of the features for higher orders ? .get_params()
does not show any list of features.
Advertisement
Answer
By the way, there is more appropriate function now: PolynomialFeatures.get_feature_names.
JavaScript
x
12
12
1
from sklearn.preprocessing import PolynomialFeatures
2
import pandas as pd
3
import numpy as np
4
5
data = pd.DataFrame.from_dict({
6
'x': np.random.randint(low=1, high=10, size=5),
7
'y': np.random.randint(low=-1, high=1, size=5),
8
})
9
10
p = PolynomialFeatures(degree=2).fit(data)
11
print p.get_feature_names(data.columns)
12
This will output as follows:
JavaScript
1
2
1
['1', 'x', 'y', 'x^2', 'x y', 'y^2']
2
N.B. For some reason you gotta fit your PolynomialFeatures object before you will be able to use get_feature_names().
If you are Pandas-lover (as I am), you can easily form DataFrame with all new features like this:
JavaScript
1
3
1
features = DataFrame(p.transform(data), columns=p.get_feature_names(data.columns))
2
print features
3
Result will look like this:
JavaScript
1
7
1
1 x y x^2 x y y^2
2
0 1.0 8.0 -1.0 64.0 -8.0 1.0
3
1 1.0 9.0 -1.0 81.0 -9.0 1.0
4
2 1.0 1.0 0.0 1.0 0.0 0.0
5
3 1.0 6.0 0.0 36.0 0.0 0.0
6
4 1.0 5.0 -1.0 25.0 -5.0 1.0
7