1. 程式人生 > >2.2sklearn.preprocessing.PolynomialFeatures生成交叉特征

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

inter 交叉 roc 標識 mod lse 參數說明 python range

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.]])

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