多项式特征生成--PolynomialFeatures类

PolynomialFeatures

sklearn.preprocessing.PolynomialFeatures(degree=2, *, interaction_only=False, include_bias=True, order='C')

生成多项式和交互特征
生成一个新的特征矩阵,由所有的阶小于等于参数degree的多项式特征组合而成

  1. 比如,如果输入的样本是二维的数据: [ a , b ] [a,b] [a,b],参数degree=2,那么生成的二项式特征为 [ 1 , a , b , a 2 , a b , b 2 ] . [1, a, b, a^2, ab, b^2]. [1,a,b,a2,ab,b2].
  2. 为什么要生成多项式特征?
    有些情况下,获取训练数据的代价经常是非常高昂的,而且从已知数据中挖掘出更多特征也不是一件容易得事情,所以我们可以用纯数学的方法来人为的制造一些特征,比如,原来的输入特征只有 x 1 , x 2 x_1,x_2 x1,x2,其对应的多项式特征有: x 1 , x 2 , x 1 x 2 , x 1 2 , x 2 2 x_1,x_2,x_1x_2,x_1^2,x_2^2 x1,x2,x1x2,x12,x22

参数

degree

int or tuple (min_degree, max_degree), default=2

数据类型描述
int指定了多项式特征的最高阶数
tuple (min_degree, max_degree)指定多项式特征的阶数范围

interaction_only

bool, default=False
如果为真,只生成交互特征(由不同特征生成的多项式特征,其阶数小于参数degree且不同于输入特征)
假设输入数据有两列特征(x,y),那么当该参数为True时,多项式特征的生成情况如下

生成情况描述
生成 x , y , x y x,y,xy x,y,xy
不生成 x 2 , y 2 x^2,y^2 x2,y2

include_bias

bool, default=True
如果为真,引入一个偏差数据列,其中所有多项式幂都为零

order

{‘C’, ‘F’}, default=’C’
在密集情况下输出数组的顺序,

属性

powers_

ndarray of shape (n_output_features_, n_features_in_)
每个输入数据的指数

n_features_in_

int
拟合过程中的特征数量

feature_names_in_

ndarray of shape (n_features_in_,)
拟合过程中的特征名称

n_output_features_

int
多项式特征数量

方法

fit(X[, y])

计算输出特征的数量

Compute number of output features.

fit_transform(X[, y])

拟合并转化数据

Fit to data, then transform it.

get_feature_names([input_features])

返回数据特征名称(将在sklearn 1.2版本中被弃用)

DEPRECATED: get_feature_names is deprecated in 1.0 and will be removed in 1.2.

get_feature_names_out([input_features])

返回输出特征的名称

Get output feature names for transformation.

get_params([deep])

返回模型参数

Get parameters for this estimator.

set_params(**params)

设置模型参数

Set the parameters of this estimator.

transform(X)

将数据转化为多项式特征

Transform data to polynomial features.

应用示例

import numpy as np
from sklearn.preprocessing import PolynomialFeatures
x=np.array([0,1])
poly=PolynomialFeatures(2)
poly.fit_transform(x)
>>> array([1.,0.,1.,0.,0.,1.])   # 1,a,b,a^2,ab,b^2
poly1=PolynomialFeatures(2,interaction_only=True)
poly1.fit_transform(x)
>>> array([1.,0.,1.,0.])    # 1,a,b,ab
多项式非线性回归是一种利用多项式函数模拟非线性关系的回归方法,其中PolynomialFeatures是一个用于生成多项式特征。它可以将原始特征转换为多项式特征,从而使非线性关系更容易被模拟。 例如,如果我们有一个包含两个特征的数据集 x = [a, b],我们可以使用PolynomialFeatures将其转换为包含原始特征和它们的交叉项、平方项等的多项式特征。这样可以得到新的特征矩阵 X = [[1, a, b, a^2, ab, b^2], ...],其中每行表示一个样本的特征。然后我们可以使用线性回归模型对新的特征矩阵进行拟合,从而得到一个多项式回归模型。 使用PolynomialFeatures的主要步骤如下: 1. 实例化一个PolynomialFeatures对象,指定要生成多项式的阶数(默认为2)。 2. 将原始特征矩阵 X 传递给PolynomialFeatures的fit_transform方法,将其转换为多项式特征矩阵。 3. 使用转换后的特征矩阵 X 和目标变量 y 训练回归模型。 示例代码如下: ```python from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression # 原始特征矩阵 X = [[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]] # 目标变量 y = [0.3, 0.7, 1.1] # 实例化PolynomialFeatures对象,生成二次多项式特征 poly = PolynomialFeatures(degree=2) # 将特征矩阵转换为多项式特征矩阵 X_poly = poly.fit_transform(X) # 实例化LinearRegression对象,拟合数据 model = LinearRegression() model.fit(X_poly, y) # 打印模型系数和截距 print(model.coef_, model.intercept_) ``` 输出结果为: ```python [ 0. 1.09999999 -0.19999998 -1.69999993 2.99999985 -1.49999984] 0.30000000000000284 ``` 其中,模型系数是一个包含所有特征的数组,截距是一个常数。可以看到,模型系数包含了原始特征的一次项、二次项和交叉项,这样可以更好地拟合非线性关系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夺笋123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值