特征选择 Feature selection
- 1.13 特征选择 Feature selection
1.13 特征选择 Feature selection
1.13.1. Removing features with low variance(删除第方差的特征)
VarianceThreshold
是一个是一种简单的特征选择基线方法。它将移除方差未达到某个阈值的所有要素。默认情况下,它会删除所有零方差特征,即在所有样本中具有相同值的特征。
例如,假设我们有一个包含布尔特征的数据集,并且我们希望删除超过 80% 的样本中为 1 或 0(开或关)的所有特征。布尔特征是伯努利随机变量,这些变量的方差由下式给出
[mathrm{Var}[X] = p(1 p)]
因此,我们可以使用阈值 .8 * (1 .8) 进行选择:
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)))
sel.fit_transform(X)
正如预期的那样,
VarianceThreshold
删除了第一列,该列的概率 (p = 5/6 > .8) 包含零。
1.13.2. Univariate feature selection(单变量特征选择)
单变量特征选择的工作原理是根据单变量统计检验选择最佳特征。它可以看作是估计器的预处理步骤。Scikit-learn将特征选择例程公开为实现转换方法的对象:
SelectKBest
: 删除除(k)最高得分功能之外的所有features
SelectPercentile
: 删除除用户指定的最高得分百分比之外的所有features
对每个特征使用常见的单变量统计测试:假阳率(FPR、false positive rate)SelectFpr
、误报率 (FDR、false positive rate,所有发现中发生了错误所占的比率)SelectFdr
或多重比较(family wise error,在一系列假设检验中,至少得出一次错误结论的 概率)SelectFwe
。
GenericUnivariateSelect
允许使用可配置的策略执行单变量特征选择。这允许使用超参数搜索估计器选择最佳单变量选择策略。
例如,我们可以使用 F 检验来检索数据集的两个最佳特征,如下所示:
from sklearn.datasets import load_iris
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_classif
X, y = load_iris(return_X_y=True)
X.shape
X_new = SelectKBest(f_classif, k=2).fit_transform(X, y)
X_new.shape
这些对象将返回单变量分数和 p 值(或仅返回
SelectKBes
t 和SelectPercentile
的分数)的评分函数作为输入:
对于回归:r_regression
、f_regression
mutual_info_regression
分类:chi2
、f_classif
、mutual_info_classif
基于F检验
的方法估计两个随机变量之间的线性依赖程度。
另一方面,互信息方法
可以捕获任何类型的统计依赖性,但由于是非参数的,它们需要更多的样本才能准确估计。
请注意,(chi^2)-检验
仅应用于非负特征,例如频率。
使用稀疏数据选择要素
如果您使用稀疏数据(即表示为稀疏矩阵的数据),chi2、mutual_info_regression mutual_info_classif将处理数据而不会使其密集。
警告 请注意不要将回归评分函数用于分类问题,您将获得无用的结果。
- Comparison of F-test and mutual information
# https://scikit-learn.org/stable/auto_examples/feature_selection/plot_f_test_vs_mi.html#sphx-glr-auto-examples-feature-selection-plot-f-test-vs-mi-py
import matplotlib.pyplot as plt
import numpy as np
from sklearn.feature_selection import f_regression, mutual_info_regression
np.random.seed(0)
X = np.random.rand(1000, 3)
y = X[:, 0] + np.sin(6 * np.pi * X[:, 1]) + 0.1 * np.random.randn(1000)
f_test, _ = f_regression(X, y)
f_test /= np.max(f_test)
mi = mutual_info_regression(X, y)
mi /= np.max(mi)
plt.figure(figsize=(15, 5))
for i in range(3):
plt.subplot(1, 3, i + 1)
plt.scatter(X[:, i], y, edgecolor="black", s=20)
plt.xlabel("$x_{}$".format(i + 1), fontsize=14)
if i == 0:
plt.ylabel("$y$", fontsize=14)
plt.title("F-test={:.2f}, MI={:.2f}".format(f_test[i], mi[i]), fontsize=16)
plt.show()
1.13.3. Recursive feature elimination(递归特征消除)
为一个外部估计器的特征分配权重(例如,线性模型的系数),
递归特征消除
(RFE
) 的目标是通过递归收缩越来越小的特征集来选择特征。
首先,根据初始特征集对估计器进行训练,并且通过任何特定属性(例如coef_、feature_importances_)或可调用属性获得每个特征的重要性。
然后,从当前特征集中修剪最不重要的特征。
该过程在修剪后的数据集上递归重复,直到最终达到要选择的所需特征数。
RFECV
在交叉验证循环中执行 RFE,以找到最佳数量的要素。
from sklearn.datasets import make_friedman1
from sklearn.feature_selection import RFECV
from sklearn.svm import SVR
X, y = make_friedman1(n_samples=50, n_features=10, random_state=0)
estimator = SVR(kernel="linear")
selector = RFECV(estimator, step=1