【Scikit-Learn 中文文档】特征选择 - 监督学习 - 用户指南 | ApacheCN
2017-11-23 17:10
591 查看
中文文档: http://sklearn.apachecn.org/cn/stable/modules/feature_selection.html
英文文档: http://sklearn.apachecn.org/en/stable/modules/feature_selection.html
官方文档: http://scikit-learn.org/stable/
GitHub: https://github.com/apachecn/scikit-learn-doc-zh(觉得不错麻烦给个
Star,我们一直在努力)
贡献者: https://github.com/apachecn/scikit-learn-doc-zh#贡献者
关于我们: http://www.apachecn.org/organization/209.html
在
selection)和降维(dimensionality reduction ),这将会提高估计器的准确度或者增加他们在高维数据集上的性能。
例如,假设我们有一个特征是布尔值的数据集,我们想要移除那些在整个数据集中特征值为0或者为1的比例超过80%的特征。布尔特征是伯努利( Bernoulli )随机变量,变量的方差为
因此,我们可以使用阈值 ``.8 * (1 - .8)``进行选择:
>>>
正如预期一样,VarianceThreshold 移除了第一列, 它的值为0的概率为
.
单变量的特征选择是通过基于单变量的统计测试来选择最好的特征。它可以当做是评估器的预处理步骤。Scikit-learn将特征选择的内容作为实现了transform方法的对象:
:class:`SelectKBest`移除那些除了评分最高的K个特征之外的所有特征
using common univariate statistical tests for each feature: false positive rate
false discovery rate
or family wise error
例如下面的实例,我们可以使用
检验样本集来选择最好的两个特征:
>>>
这些对象将得分函数作为输入,返回单变量的得分和p值 (或者仅仅是
对于回归:
对于分类:
这些基于F-test的方法计算两个随机变量之间的线性相关程度。另一方面,mutual information methods能够计算任何种类的统计相关性,但是是非参数的,需要更多的样本来进行准确的估计。
稀疏数据的特征选择
如果你使用的是稀疏的数据 (用稀疏矩阵来表示数据),
Warning
不要使用一个回归得分函数来处理分类问题,你会得到无用的结果。
Examples:
Univariate
Feature Selection
Comparison
of F-test and mutual information
给定一个外部的估计器,将特征设置一定的权重 (比如,线性模型的相关系数), recursive feature elimination (
通过考虑越来越小的特征集合来递归的选择特征。 首先,训练器在初始的特征集合上面训练并且每一个特征的重要程度是通过一个
或者
来找到最优的特征数量
Examples:
Recursive
feature elimination: A recursive feature elimination example showing the relevance of pixels in a digit classification task.
Recursive
feature elimination with cross-validation: A recursive feature elimination example with automatic tuning of the number of features selected with cross-validation.
,它可以用来处理任何带有
如果相关的``coef_`` or
有关如何使用的例子,可以参阅下面的例子。
Examples
Feature
selection using SelectFromModel and LassoCV: Selecting the two most important features from the Boston dataset without knowing the threshold beforehand.
Linear models 使用L1正则化的线性模型会得到稀疏解:他们的许多系数为0。
当目标是降低使用另一个分类器的数据集的纬度, 他们可以与
以及
分类器:class:svm.LinearSVC
>>>
在svm和逻辑回归中,参数C是用来控制稀疏性的:小的C会导致少的特征被选择。使用Lasso,alpha的值越大, 越少的特征会被选择。
示例:
Classification
of text documents using sparse features: Comparison of different algorithms for document classification including L1-based feature selection.
L1-recovery and compressive sensing
For a good choice of alpha, the Lasso can
fully recover the exact set of non-zero variables using only few observations, provided certain specific conditions are met. In particular, the number of samples should be “sufficiently large”, or L1 models will perform at random, where “sufficiently large”
depends on the number of non-zero coefficients, the logarithm of the number of features, the amount of noise, the smallest absolute value of non-zero coefficients, and the structure of the design matrix X. In addition, the design matrix must display certain
specific properties, such as not being too correlated.
There is no general rule to select an alpha parameter for recovery of non-zero coefficients. It can by set by cross-validation (
though this may lead to under-penalized models: including a small number of non-relevant variables is not detrimental to prediction score. BIC (
tends, on the opposite, to set high values of alpha.
Reference Richard G. Baraniuk “Compressive Sensing”, IEEE Signal Processing Magazine [120] July 2007http://dsp.rice.edu/sites/dsp.rice.edu/files/cs/baraniukCSlecture07.pdf
基于树的estimators (查阅
在
可以用来计算特征的重要性,然后可以消除不相关的特征 (when coupled with the
>>>
Examples:
Feature
importances with forests of trees: example on synthetic data showing the recovery of the actually meaningful features.
Pixel
importances with a parallel forest of trees: example on face recognition data.
特征选择通常在实际的学习之前用来做预处理。在scikit-learn中推荐的方式是使用 :
在这个小节中,我们利用
然后,在转化后的输出中使用一个
比如只使用相关的特征。你可以使用其他特征选择的方法和提供评估特征重要性的分类器执行相似的操作。 请查阅
的实例。
中文文档: http://sklearn.apachecn.org/cn/stable/modules/feature_selection.html
英文文档: http://sklearn.apachecn.org/en/stable/modules/feature_selection.html
官方文档: http://scikit-learn.org/stable/
GitHub: https://github.com/apachecn/scikit-learn-doc-zh(觉得不错麻烦给个
Star,我们一直在努力)
贡献者: https://github.com/apachecn/scikit-learn-doc-zh#贡献者
关于我们: http://www.apachecn.org/organization/209.html
有兴趣的们也可以和我们一起来维护,持续更新中 。。。
机器学习交流群: 629470233
英文文档: http://sklearn.apachecn.org/en/stable/modules/feature_selection.html
官方文档: http://scikit-learn.org/stable/
GitHub: https://github.com/apachecn/scikit-learn-doc-zh(觉得不错麻烦给个
Star,我们一直在努力)
贡献者: https://github.com/apachecn/scikit-learn-doc-zh#贡献者
关于我们: http://www.apachecn.org/organization/209.html
1.13. 特征选择
在 sklearn.feature_selection模块中的类可以用来对样本集进行特征选择(feature
selection)和降维(dimensionality reduction ),这将会提高估计器的准确度或者增加他们在高维数据集上的性能。
1.13.1. 移除低方差特征
VarianceThreshold是特征选择的一个简单基本方法,它会移除所有那些方差不满足一些阈值的特征。默认情况下,它将会移除所有的零方差特征,比如,特征在所有的样本上的值都是一样的(即方差为0)。
例如,假设我们有一个特征是布尔值的数据集,我们想要移除那些在整个数据集中特征值为0或者为1的比例超过80%的特征。布尔特征是伯努利( Bernoulli )随机变量,变量的方差为
因此,我们可以使用阈值 ``.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) array([[0, 1], [1, 0], [0, 0], [1, 1], [1, 0], [1, 1]])
正如预期一样,VarianceThreshold 移除了第一列, 它的值为0的概率为
.
1.13.2. 单变量特征选择
单变量的特征选择是通过基于单变量的统计测试来选择最好的特征。它可以当做是评估器的预处理步骤。Scikit-learn将特征选择的内容作为实现了transform方法的对象::class:`SelectKBest`移除那些除了评分最高的K个特征之外的所有特征
SelectPercentile移除除了用户指定的最高得分百分比之外的所有特征
using common univariate statistical tests for each feature: false positive rate
SelectFpr,
false discovery rate
SelectFdr,
or family wise error
SelectFwe.
GenericUnivariateSelect允许使用可配置方法来进行单变量特征选择。它允许超参数搜索评估器来选择最好的单变量特征。
例如下面的实例,我们可以使用
检验样本集来选择最好的两个特征:
>>>
>>> from sklearn.datasets import load_iris >>> from sklearn.feature_selection import SelectKBest >>> from sklearn.feature_selection import chi2 >>> iris = load_iris() >>> X, y = iris.data, iris.target >>> X.shape (150, 4) >>> X_new = SelectKBest(chi2, k=2).fit_transform(X, y) >>> X_new.shape (150, 2)
这些对象将得分函数作为输入,返回单变量的得分和p值 (或者仅仅是
SelectKBest和
SelectPercentile的分数):
对于回归:
f_regression,
mutual_info_regression
对于分类:
chi2,
f_classif,
mutual_info_classif
这些基于F-test的方法计算两个随机变量之间的线性相关程度。另一方面,mutual information methods能够计算任何种类的统计相关性,但是是非参数的,需要更多的样本来进行准确的估计。
稀疏数据的特征选择
如果你使用的是稀疏的数据 (用稀疏矩阵来表示数据),
chi2,
mutual_info_regression,
mutual_info_classif处理数据时不会使它变密集。
Warning
不要使用一个回归得分函数来处理分类问题,你会得到无用的结果。
Examples:
Univariate
Feature Selection
Comparison
of F-test and mutual information
1.13.3. 递归特征消除
给定一个外部的估计器,将特征设置一定的权重 (比如,线性模型的相关系数), recursive feature elimination (RFE)
通过考虑越来越小的特征集合来递归的选择特征。 首先,训练器在初始的特征集合上面训练并且每一个特征的重要程度是通过一个
coef_属性
或者
feature_importances_属性. 然后,从当前的特征集合中移除最不重要的特征。在特征集合上不断的重复递归这个步骤,知道达到所需要的特征数量为止。
RFECV在一个交叉验证的循环中执行RFE
来找到最优的特征数量
Examples:
Recursive
feature elimination: A recursive feature elimination example showing the relevance of pixels in a digit classification task.
Recursive
feature elimination with cross-validation: A recursive feature elimination example with automatic tuning of the number of features selected with cross-validation.
1.13.4. 使用 SelectFromModel 选取特征
SelectFromModel是一个meta-transformer
,它可以用来处理任何带有
coef_或者
feature_importances_属性的训练之后的训练器。
如果相关的``coef_`` or
featureimportances属性值低于预先设置的阈值,这些特征将会被认为不重要并且移除掉。除了指定数值上的阈值之外,还可以使用启发式的方法用字符串参数来找到一个合适的阈值。可以使用的启发式方法有mean、median以及使用浮点数乘以这些(例如,0.1*mean)。
有关如何使用的例子,可以参阅下面的例子。
Examples
Feature
selection using SelectFromModel and LassoCV: Selecting the two most important features from the Boston dataset without knowing the threshold beforehand.
1.13.4.1. 基于 L1 的特征选取
Linear models 使用L1正则化的线性模型会得到稀疏解:他们的许多系数为0。当目标是降低使用另一个分类器的数据集的纬度, 他们可以与
feature_selection.SelectFromModel一起使用来选择非零系数。特别的,用于此目的的稀疏估计量是用于回归的
linear_model.Lasso,
以及
linear_model.LogisticRegression和
分类器:class:svm.LinearSVC
>>>
>>> from sklearn.svm import LinearSVC >>> from sklearn.datasets import load_iris >>> from sklearn.feature_selection import SelectFromModel >>> iris = load_iris() >>> X, y = iris.data, iris.target >>> X.shape (150, 4) >>> lsvc = LinearSVC(C=0.01, penalty="l1", dual=False).fit(X, y) >>> model = SelectFromModel(lsvc, prefit=True) >>> X_new = model.transform(X) >>> X_new.shape (150, 3)
在svm和逻辑回归中,参数C是用来控制稀疏性的:小的C会导致少的特征被选择。使用Lasso,alpha的值越大, 越少的特征会被选择。
示例:
Classification
of text documents using sparse features: Comparison of different algorithms for document classification including L1-based feature selection.
L1-recovery and compressive sensing
For a good choice of alpha, the Lasso can
fully recover the exact set of non-zero variables using only few observations, provided certain specific conditions are met. In particular, the number of samples should be “sufficiently large”, or L1 models will perform at random, where “sufficiently large”
depends on the number of non-zero coefficients, the logarithm of the number of features, the amount of noise, the smallest absolute value of non-zero coefficients, and the structure of the design matrix X. In addition, the design matrix must display certain
specific properties, such as not being too correlated.
There is no general rule to select an alpha parameter for recovery of non-zero coefficients. It can by set by cross-validation (
LassoCVor
LassoLarsCV),
though this may lead to under-penalized models: including a small number of non-relevant variables is not detrimental to prediction score. BIC (
LassoLarsIC)
tends, on the opposite, to set high values of alpha.
Reference Richard G. Baraniuk “Compressive Sensing”, IEEE Signal Processing Magazine [120] July 2007http://dsp.rice.edu/sites/dsp.rice.edu/files/cs/baraniukCSlecture07.pdf
1.13.4.2. 基于 Tree(树)的特征选取
基于树的estimators (查阅 sklearn.tree模块和树的森林
在
sklearn.ensemble模块)
可以用来计算特征的重要性,然后可以消除不相关的特征 (when coupled with the
sklearn.feature_selection.SelectFromModelmeta-transformer):
>>>
>>> from sklearn.ensemble import ExtraTreesClassifier >>> from sklearn.datasets import load_iris >>> from sklearn.feature_selection import SelectFromModel >>> iris = load_iris() >>> X, y = iris.data, iris.target >>> X.shape (150, 4) >>> clf = ExtraTreesClassifier() >>> clf = clf.fit(X, y) >>> clf.feature_importances_ array([ 0.04..., 0.05..., 0.4..., 0.4...]) >>> model = SelectFromModel(clf, prefit=True) >>> X_new = model.transform(X) >>> X_new.shape (150, 2)
Examples:
Feature
importances with forests of trees: example on synthetic data showing the recovery of the actually meaningful features.
Pixel
importances with a parallel forest of trees: example on face recognition data.
1.13.5. 特征选取作为 pipeline(管道)的一部分
特征选择通常在实际的学习之前用来做预处理。在scikit-learn中推荐的方式是使用 :sklearn.pipeline.Pipeline:
clf = Pipeline([ ('feature_selection', SelectFromModel(LinearSVC(penalty="l1"))), ('classification', RandomForestClassifier()) ]) clf.fit(X, y)
在这个小节中,我们利用
sklearn.svm.LinearSVC和
sklearn.feature_selection.SelectFromModel来评估特征的重要性并且选择出相关的特征。
然后,在转化后的输出中使用一个
sklearn.ensemble.RandomForestClassifier分类器,
比如只使用相关的特征。你可以使用其他特征选择的方法和提供评估特征重要性的分类器执行相似的操作。 请查阅
sklearn.pipeline.Pipeline更多
的实例。
中文文档: http://sklearn.apachecn.org/cn/stable/modules/feature_selection.html
英文文档: http://sklearn.apachecn.org/en/stable/modules/feature_selection.html
官方文档: http://scikit-learn.org/stable/
GitHub: https://github.com/apachecn/scikit-learn-doc-zh(觉得不错麻烦给个
Star,我们一直在努力)
贡献者: https://github.com/apachecn/scikit-learn-doc-zh#贡献者
关于我们: http://www.apachecn.org/organization/209.html
有兴趣的们也可以和我们一起来维护,持续更新中 。。。
机器学习交流群: 629470233
相关文章推荐
- 【Scikit-Learn 中文文档】特征选择 - 监督学习 - 用户指南 | ApacheCN
- 【Scikit-Learn 中文文档】十五:特征选择 - 监督学习 - 用户指南 | ApacheCN
- 【Scikit-Learn 中文文档】特征选择 - 监督学习 - 用户指南 | ApacheCN
- 【Scikit-Learn 中文文档】特征选择 - 监督学习 - 用户指南 | ApacheCN
- 【Scikit-Learn 中文文档】特征选择 - 监督学习 - 用户指南 | ApacheCN
- 【Scikit-Learn 中文文档】特征选择 - 监督学习 - 用户指南 | ApacheCN
- 【Scikit-Learn 中文文档】特征选择 - 监督学习 - 用户指南 | ApacheCN
- 【Scikit-Learn 中文文档】15 特征选择 - 监督学习 - 用户指南 | ApacheCN
- 【Scikit-Learn 中文文档】特征选择 - 监督学习 - 用户指南 | ApacheCN
- 【Scikit-Learn 中文文档】特征选择 - 监督学习 - 用户指南 | ApacheCN
- 【Scikit-Learn 中文文档】特征选择 - 监督学习 - 用户指南 | ApacheCN
- 【Scikit-Learn 中文文档】特征选择 - 监督学习 - 用户指南 | ApacheCN
- 【Scikit-Learn 中文文档】特征选择 - 监督学习 - 用户指南 | ApacheCN
- 【Scikit-Learn 中文文档】新异类和异常值检测 - 无监督学习 - 用户指南 | ApacheCN
- 【Scikit-Learn 中文文档】新异类和异常值检测 - 无监督学习 - 用户指南 | ApacheCN
- 【Scikit-Learn 中文文档】广义线性模型 - 监督学习 - 用户指南 | ApacheCN
- 【Scikit-Learn 中文文档】广义线性模型 - 监督学习 - 用户指南 | ApacheCN
- 【Scikit-Learn 中文文档】线性和二次判别分析 - 监督学习 - 用户指南 | ApacheCN
- 【Scikit-Learn 中文文档】神经网络模型(无监督)- 无监督学习 - 用户指南 | ApacheCN
- 【Scikit-Learn 中文文档】密度估计 - 无监督学习 - 用户指南 | ApacheCN