# 【2】监督学习--3--多项式变形--PolynomialFeatures

sklearn.preprocessing.PolynomialFeatures(degree=2, interaction_only=False, include_bias=True)


PolynomialFeatures函数包含了3个参数和3个属性

• degree ： 项的指数和，默认为2。如果为2，如果输入的为[a,b]，则输出为 [1, a, b, a^2, ab, b^2]
• interaction_only ： 默认的是False，如果为True，则每个式子中包含自己只有1次，不包含x[1] ** 2, x[0] * x[2] ** 3等
• include_bias : boolean 。 If True (default), then include a bias column, the feature in which all polynomial powers are zero (i.e. a column of ones - acts as an intercept term in a linear model).

• powers_ ：可以看到多项式取值的方式
• n_input_features_ : int 。The total number of input features.
• n_output_features_ : int 。 The total number of polynomial output features. The number of output features is computed by iterating over all suitably sized combinations of input features.

import numpy as np
from sklearn.preprocessing import  PolynomialFeatures

X = np.arange(6).reshape(3, 2)

print '\nresult1:'
print X

poly = PolynomialFeatures(2)

print '\nresult2:'
print poly.fit_transform(X)

print '\nresult2_powers:'
print poly.powers_
print '\nresult2_input_features:'
print poly.n_input_features_
print '\nresult2_output_features:'
print poly.n_output_features_

X = np.arange(6).reshape(2, 3)

poly = PolynomialFeatures(degree =4,interaction_only=True) #同一个自己只能出现一次
print '\nresult3:'
print poly.fit_transform(X)
print '\nresult3_powers:'
print poly.powers_

poly = PolynomialFeatures(degree =1)
print '\nresult4:'
print poly.fit_transform(X)
print '\nresult4_powers:'
print poly.powers_


result1:
[[0 1]
[2 3]
[4 5]]

result2:
[[ 1.  0.  1.  0.  0.  1.]
[ 1.  2.  3.  4.  6.  9.]
[ 1.  4.  5. 16. 20. 25.]]

result2_powers:
[[0 0]
[1 0]
[0 1]
[2 0]
[1 1]
[0 2]]

result2_input_features:
2

result2_output_features:
6

result3:
[[ 1.  0.  1.  2.  0.  0.  2.  0.]
[ 1.  3.  4.  5. 12. 15. 20. 60.]]

result3_powers:
[[0 0 0]
[1 0 0]
[0 1 0]
[0 0 1]
[1 1 0]
[1 0 1]
[0 1 1]
[1 1 1]]

result4:
[[1. 0. 1. 2.]
[1. 3. 4. 5.]]

result4_powers:
[[0 0 0]
[1 0 0]
[0 1 0]
[0 0 1]]


## 参考资料

http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.PolynomialFeatures.html