Sklearn.preprocessing.PolynomialFeatures Original
The polynomial generation function:sklearn.preprocessing.PolynomialFeatures(degree=2, interaction_only=False, include_bias=True)
Parameter description:
degree
: Default is 2, polynomial count (same number of times in several equations)
interaction_only
: Whether to include a single argument **n (n>1) feature data ID, default to False, true to remove the case of multiplying itself
include_bias
: whether to include the deviation identifier, which is true by default, or false to indicate that no deviation is included
import numpy as npfrom 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 Generating Crossover features