Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

python - sklearn: how to get coefficients of polynomial features

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

By the way, there is more appropriate function now: PolynomialFeatures.get_feature_names.

from sklearn.preprocessing import PolynomialFeatures
import pandas as pd
import numpy as 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 will output as follows:

['1', 'x', 'y', 'x^2', 'x y', 'y^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:

features = DataFrame(p.transform(data), columns=p.get_feature_names(data.columns))
print features

Result will look like 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...