python - sklearn: how to get coefficients of polynomial features -
i know possible obtain polynomial features numbers using: polynomial_features.transform(x). according manual, degree of 2 features are: [1, a, b, a^2, ab, b^2]. how obtain description of features higher orders ? .get_params() not show list of features.
by way, there more appropriate function now: polynomialfeatures.get_feature_names.
from sklearn.preprocessing import polynomialfeatures import pandas pd import numpy np data = pd.dataframe.from_dict({ 'x': np.random.randint(low=1, high=10, size=5), 'y': np.random.randint(low=-1, high=1, size=5), }) p = polynomialfeatures(degree=2).fit(data) print p.get_feature_names(data.columns) this output follows:
['1', 'x', 'y', 'x^2', 'x y', 'y^2'] n.b. reason gotta fit polynomialfeatures object before able use get_feature_names().
if pandas-lover (as am), can form dataframe new features this:
features = dataframe(p.transform(data), columns=p.get_feature_names(data.columns)) print features result this:
1 x y x^2 x y y^2 0 1.0 8.0 -1.0 64.0 -8.0 1.0 1 1.0 9.0 -1.0 81.0 -9.0 1.0 2 1.0 1.0 0.0 1.0 0.0 0.0 3 1.0 6.0 0.0 36.0 0.0 0.0 4 1.0 5.0 -1.0 25.0 -5.0 1.0
Comments
Post a Comment