【Scikit-Learn 中文文檔】特徵選擇 - 監督學習 - 用戶指南 | ApacheCN

中文文檔: 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




1.13. 特徵選擇

在 sklearn.feature_selection 模塊中的類可以用來對樣本集進行特徵選擇(feature selection)和降維(dimensionality reduction ),這將會提高估計器的準確度或者增加他們在高維數據集上的性能。

1.13.1. 移除低方差特徵

VarianceThreshold 是特徵選擇的一個簡單基本方法,它會移除所有那些方差不滿足一些閾值的特徵。默認情況下,它將會移除所有的零方差特徵,比如,特徵在所有的樣本上的值都是一樣的(即方差爲0)。

例如,假設我們有一個特徵是布爾值的數據集,我們想要移除那些在整個數據集中特徵值爲0或者爲1的比例超過80%的特徵。布爾特徵是伯努利( Bernoulli )隨機變量,變量的方差爲

\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)
array([[0, 1],
       [1, 0],
       [0, 0],
       [1, 1],
       [1, 0],
       [1, 1]])

正如預期一樣,VarianceThreshold 移除了第一列, 它的值爲0的概率爲 p = 5/6 > .8 .

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 允許使用可配置方法來進行單變量特徵選擇。它允許超參數搜索評估器來選擇最好的單變量特徵。

例如下面的實例,我們可以使用 \chi^2 檢驗樣本集來選擇最好的兩個特徵:

>>>
>>> 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-test的方法計算兩個隨機變量之間的線性相關程度。另一方面,mutual information methods能夠計算任何種類的統計相關性,但是是非參數的,需要更多的樣本來進行準確的估計。

稀疏數據的特徵選擇

如果你使用的是稀疏的數據 (用稀疏矩陣來表示數據),
chi2mutual_info_regressionmutual_info_classif 處理數據時不會使它變密集。

Warning

   

不要使用一個迴歸得分函數來處理分類問題,你會得到無用的結果。

1.13.3. 遞歸特徵消除

給定一個外部的估計器,將特徵設置一定的權重 (比如,線性模型的相關係數), recursive feature elimination (RFE) 通過考慮越來越小的特徵集合來遞歸的選擇特徵。 首先,訓練器在初始的特徵集合上面訓練並且每一個特徵的重要程度是通過一個 coef_ 屬性 或者 feature_importances_ 屬性. 然後,從當前的特徵集合中移除最不重要的特徵。在特徵集合上不斷的重複遞歸這個步驟,知道達到所需要的特徵數量爲止。 RFECV 在一個交叉驗證的循環中執行RFE 來找到最優的特徵數量

Examples:

1.13.4. 使用 SelectFromModel 選取特徵

SelectFromModel 是一個meta-transformer ,它可以用來處理任何帶有 coef_ 或者 feature_importances_ 屬性的訓練之後的訓練器。 如果相關的``coef_`` or featureimportances 屬性值低於預先設置的閾值,這些特徵將會被認爲不重要並且移除掉。除了指定數值上的閾值之外,還可以使用啓發式的方法用字符串參數來找到一個合適的閾值。可以使用的啓發式方法有mean、median以及使用浮點數乘以這些(例如,0.1*mean)。

有關如何使用的例子,可以參閱下面的例子。

Examples

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的值越大, 越少的特徵會被選擇。

示例:

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 (LassoCV or 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.SelectFromModel meta-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:

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章