机器学习—特征工程—多项式拓展

多项式拓展

库:sklearn.preprocessing.PolynomialFeatures

1、PolynomialFeatures内部参数:
degree:控制多项式的度

interaction_only: 默认为False,如果指定为True,那么就不会有特征自己和自己结合的项,以ab两项为例的话,拓展出的项就没有a^2和b^2。

include_bias:默认为True。如果为True的话,前边有一列全为1的偏置项
2、代码实现
import pandas as pd
a = pd.DataFrame([[1,2,3],
                  [4,5,6],
                  [1,8,9]],columns = ["feature_1", "feature_2", "label"])
#导库
from sklearn.preprocessing import PolynomialFeatures

#最高次项为2  有一列为1的偏置项 有自己与自己相乘 
polyCoder = PolynomialFeatures(degree=2, include_bias=True, interaction_only=False)
df = polyCoder.fit_transform(a)
print(pd.DataFrame(df, columns=polyCoder.get_feature_names()))

结果展示

     1   x0   x1   x2  x0^2  x0 x1  x0 x2  x1^2  x1 x2  x2^2
0  1.0  1.0  2.0  3.0   1.0    2.0    3.0   4.0    6.0   9.0
1  1.0  4.0  5.0  6.0  16.0   20.0   24.0  25.0   30.0  36.0
2  1.0  1.0  8.0  9.0   1.0    8.0    9.0  64.0   72.0  81.0
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值