特征选择

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 分类:

posted @ 2019-09-07 17:37  大脸猫12581  阅读(304)  评论(0编辑  收藏  举报