特征选择

1、VarianceThreshold()---删除方差低的要素

是一种简单的特征选择基线方法。它会删除方差不符合某个阈值的所有要素。默认情况下,它会删除所有零方差要素,即在所有样本中具有相同值的要素。

import numpy as np
import pandas as pd
from sklearn.feature_selection import VarianceThreshold
X = [[0, 0, 1], [0, 1, 0], [1, 0, 0], [0, 1, 1], [0, 1, 0], [0, 1, 1]]
sel = VarianceThreshold(threshold=(.8 * (1 - .8)))
print(sel.fit_transform(X))
'''
[[0 1]
 [1 0]
 [0 0]
 [1 1]
 [1 0]
 [1 1]]
'''

2、单变量特征选择

(1)SelectKBest()删除除了k个最高得分外的所有特征

import numpy as np
import pandas as pd
from sklearn.feature_selection import VarianceThreshold,SelectKBest,chi2
X = [[0, 0, 1], [0, 1, 0], [1, 0, 0], [0, 1, 1], [0, 1, 0], [0, 1, 1]]
y = [[1],[0],[0],[1],[0],[1]]
# sel = VarianceThreshold(threshold=(.8 * (1 - .8)))
X_new  = SelectKBest(chi2, k=2).fit_transform(X, y)
print(X_new)
# print(sel.fit_transform(X))
'''
[[0 1]
 [0 0]
 [1 0]
 [0 1]
 [0 0]
 [0 1]]
'''
(2)SelectPercentile()删除指定百分比外的所有特征
X_per = SelectPercentile(chi2,percentile=70).fit_transform(X,y)

3、递归特征消除(RFE)

通过coef_和feature_importances_获得每个特征的重要性,然后从中修剪最不重要的特征

RFECV 在交叉验证循环中执行RFE以找到最佳数量的特征。

4、使用SelectFromModel进行特征选择

(1)基于L1的特征选择

稀疏估计量linear_model.Lasso用于回归,分析linear_model.LogisticRegressionsvm.LinearSVC 分类:

转载于:https://www.cnblogs.com/Cheryol/p/11482034.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值