多项式回归简介

多项式回归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 most degree distinct input features (so not x[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
a
b^2
b^3,
a^3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值