多项式回归Polynomial()简介
1. 多项式回归简介
from sklearn.preprocessing import PolynomialFeatures
?PolynomialFeatures
Init signature: PolynomialFeatures(degree=2, *,
interaction_only=False, include_bias=True, order=‘C’) Docstring:
Generate polynomial and interaction features.Generate a new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree. For example, if an input sample is two dimensional
and of the form [a, b], the degree-2 polynomial features are [1, a, b, a^2, ab, b^2].Parameters
---------- degree : int, default=2
The degree of the polynomial features.interaction_only : bool, default=False
If true, only interaction features are produced: features that are products of at mostdegree
distinct input features (so notx[1] ** 2
,x[0] * x[2] ** 3
, etc.).include_bias : bool, default=True
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).
a=[[2,3]] #有两个自变量,一个样本
pf=PolynomialFeatures(degree=2)
print(pf.fit_transform(a))
[[1. 2. 3. 4. 6. 9.]]
pf.fit_transform(a)
参数a必须是二维矩阵
b = [2,3]
pf=PolynomialFeatures(degree=2) print(pf.fit_transform(b))ValueError: Expected 2D array, got 1D array instead: array=[2 3].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
reshape命令: np.reshape(-1,1)
将数组变为只有一列的数组,不需要事先指定有多少行
b = np.array([2,3]) #只有1个自变量,但有两个样本
b.reshape(-1,1) #让b变成只有一列,行数不知道多少
b
array([2, 3])
print(b.reshape(-1,1))
[[2]
[3]]
pf = PolynomialFeatures(degree=2)
print(pf.fit_transform(b.reshape(-1,1)))
[[1. 2. 4.]
[1. 3. 9.]]
reshape命令: np.reshape(-1,2)
将数组变为只有2列的数组,不需要事先指定有多少行
z = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]])
z.shape
(4, 4)
(4, 4)
z.reshape(-1,2)
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10],
[11, 12],
[13, 14],
[15, 16]])
1.1 PolynomialFeatures的参数设置
1.1.1 PolynomialFeatures(degree =2) 二元二次多项式
pf=PolynomialFeatures(degree=2,include_bias=False)
print(pf.fit_transform(a))
[[2. 3. 4. 6. 9.]]
pf=PolynomialFeatures(degree=2,interaction_only=True)
#interaction_only=True只包含交叉相乘项,不包含平方项
print(pf.fit_transform(a))
[[1. 2. 3. 6.]]
1.1.2 PolynomialFeatures(degree =2) 二元三次多项式
a=[[2,3]]
pf=PolynomialFeatures(degree=3)
print(pf.fit_transform(a))
[[ 1. 2. 3. 4. 6. 9. 8. 12. 18. 27.]]
二元三次多项式的因式包括:1,a,b, ab
a^2,
b^2
ba^2
ab^2
b^3,
a^3