sklearn.preprocessing.PolynomialFeatures
多项式扩展。
它是使用多项式的方法来进行的,如果有a,b两个特征,那么它的2次多项式为(1,a,b,a^2,ab, b^2),这个多项式的形式是使用poly的效果。
api
class sklearn.preprocessing.PolynomialFeatures(degree=2, interaction_only=False, include_bias=True)
参数:degree:控制多项式的度,即你是几次多项式扩展interaction_only: 默认为False,如果指定为True,那么就不会有特征自己和自己结合的项,上面的二次项中没有a^2和b^2。include_bias:默认为True。如果为True的话,那么就会有上面的1那一项。
示例
X = np.arange(6).reshape(3, 2)
print(X)
array([[0, 1],[2, 3],[4, 5]])
poly = PolynomialFeatures(2)
poly.fit_transform(X)
array([[ 1., 0., 1., 0., 0., 1.],[ 1., 2., 3., 4., 6., 9.],[ 1., 4., 5., 16., 20., 25.]]) # 符合(1,a,b,a^2,ab, b^2)
poly = PolynomialFeatures(interaction_only=True)
poly.fit_transform(X)
array([[ 1., 0., 1., 0.],[ 1., 2., 3., 6.],[ 1., 4., 5., 20.]]) # 输出中不包含a^2和b^2项