多项式拓展
库:sklearn.preprocessing.PolynomialFeatures
1、PolynomialFeatures内部参数:
degree:控制多项式的度
interaction_only: 默认为False,如果指定为True,那么就不会有特征自己和自己结合的项,以ab两项为例的话,拓展出的项就没有a^2和b^2。
include_bias:默认为True。如果为True的话,前边有一列全为1的偏置项
2、代码实现
import pandas as pd
a = pd.DataFrame([[1,2,3],
[4,5,6],
[1,8,9]],columns = ["feature_1", "feature_2", "label"])
#导库
from sklearn.preprocessing import PolynomialFeatures
#最高次项为2 有一列为1的偏置项 有自己与自己相乘
polyCoder = PolynomialFeatures(degree=2, include_bias=True, interaction_only=False)
df = polyCoder.fit_transform(a)
print(pd.DataFrame(df, columns=polyCoder.get_feature_names()))
结果展示
1 x0 x1 x2 x0^2 x0 x1 x0 x2 x1^2 x1 x2 x2^2
0 1.0 1.0 2.0 3.0 1.0 2.0 3.0 4.0 6.0 9.0
1 1.0 4.0 5.0 6.0 16.0 20.0 24.0 25.0 30.0 36.0
2 1.0 1.0 8.0 9.0 1.0 8.0 9.0 64.0 72.0 81.0