1. 基于离散程度进行筛选
移除变异度明显过低的特征
函数介绍
class sklearn.feature_selection.Variance Threshold(threshold=0.0)
默认设定将会移除模型中的常量
Variance Threshold类的属性:
variances_: array, shape (n_features,), 各特征列的方差值
代码解释
from sklearn.feature_selection import VarianceThreshold
X = [[0, 2, 0, 3],
[0, 1, 4, 3],
[0, 1, 1, 3]]
selector = VarianceThreshold()
selector.fit(X)
selector.variances_
selector.transform(X)
运行结果


从结果中可以看出,第一列与第四列已被过滤
2. 基于单变量检验进行筛选
2.1. 筛选关联程度最高的特征
可以将分析范围聚集于核心特征
函数介绍
SelectKBest: 移除那些除了评分最高的K个特征之外的所有特征,即保留前K个特征
SelectPercentile: 移除除了用户指定的最高得分百分比之外的所有特征
class sklearn.feature_selection.SelectPercentile/SelectKBest(
score_func = < function f_classif >:用于计算评分的统计方法
f_classif:ANOVA F-value , 用于类别预测
mutual_info_classif: 类别预测中的共同信息,非参方法,样本量要求高
chi2:卡方检验
f_regression: 回归分析中的F-value
mutual_info_regression:数值预测中的共同信息
precentile=10: 用于SelectPercentile,希望保留的特征比例
k = 10:用于SelectKBest,希望保留的特征数量
)
SelectPercentile/SelectKBest类的属性
scores_: array-like, shape=(n_features,)
pvalues_: array-like, shape=(n_features,)
代码解释
from sklearn import datasets
boston = datasets.load_boston()
boston.data[:2]
from sklearn.feature_selection import SelectKBest, f_regression
# 由于是连续变量,因此用f_regressi

本文介绍了使用Sklearn进行特征选择的多种方法,包括基于离散程度的筛选,如Variance Threshold,以及基于单变量检验的SelectKBest、SelectPercentile、SelectFpr/Fdr/Fwe。此外,还探讨了基于建模结果的SelecFromModel,并讨论了在特征筛选时如何选择合适的阈值。最后,文章讲解了主成分分析(PCA)在数据降维中的应用,强调PCA在处理多重共线性问题和信息浓缩方面的作用。
最低0.47元/天 解锁文章
6305

被折叠的 条评论
为什么被折叠?



