多项式特征生成--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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

夺笋123

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

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

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

打赏作者

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

抵扣说明:

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

余额充值