2.2sklearn.preprocessing.PolynomialFeatures生成交叉特征

sklearn.preprocessing.PolynomialFeatures原文

多项式生成函数:sklearn.preprocessing.PolynomialFeatures(degree=2, interaction_only=False, include_bias=True)
参数说明:

  • degree:默认为2,多项式次数(就同几元几次方程中的次数一样)
  • interaction_only:是否包含单个自变量**n(n>1)特征数据标识,默认为False,为True则表示去除与自己相乘的情况
  • include_bias:是否包含偏差标识,默认为True,为False则表示不包含偏差项
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
X = np.arange(6).reshape(3, 2)
X
array([[0, 1],
       [2, 3],
       [4, 5]])
poly = PolynomialFeatures(degree = 2)
poly.fit_transform(X)
array([[ 1.,  0.,  1.,  0.,  0.,  1.],
       [ 1.,  2.,  3.,  4.,  6.,  9.],
       [ 1.,  4.,  5., 16., 20., 25.]])
# 设置参数interaction_only = True,不包含单个自变量****n(n>1)特征数据
poly = PolynomialFeatures(degree = 2, interaction_only = True)
poly.fit_transform(X)
array([[ 1.,  0.,  1.,  0.],
       [ 1.,  2.,  3.,  6.],
       [ 1.,  4.,  5., 20.]])
# 再添加 设置参数include_bias= False,不包含偏差项数据
poly = PolynomialFeatures(degree = 2, interaction_only = True, include_bias=False)
poly.fit_transform(X)
array([[ 0.,  1.,  0.],
       [ 2.,  3.,  6.],
       [ 4.,  5., 20.]])

猜你喜欢

转载自www.cnblogs.com/cjr0707/p/9750639.html