官網:sklearn.feature_selection.SelectPercentile
函數
根據最高分數的百分位數選擇特徵。
class sklearn.feature_selection.SelectPercentile(score_func=<function f_classif>, *, percentile=10)
參數說明
Parameters
----------
score_func:callable
函數接受兩個數組X和y,並返回一對數組(分數,
pvalue)或帶分數的單個數組。默認值爲f_classif。
默認功能僅適用於分類任務。
percentile:int, optional, default=10
要保留的特徵百分比。
Attributes
----------
scores_:array-like of shape (n_features,)
Scores of features.
pvalues_:array-like of shape (n_features,)
p-values of feature scores,
None if score_func returned only scores.
Note:
分數相等的要素之間的關係將以不確定的方式斷開。
方法
'fit(self, X, y)'
在(X,y)上訓練過濾器並獲得適當的功能。
'fit_transform(self, X[, y])'
訓練過濾器,然後對X進行轉換。
'get_params(self[, deep])'
獲取此估計量的參數。
'get_support(self[, indices])'
獲取所選特徵的掩碼或整數索引
Get a mask, or integer index, of the features selected
'inverse_transform(self, X)'
反向轉換操作
'set_params(self, \*\*params)'
設置此估算器的參數。
'transform(self, X)'
將X縮小爲選定的特徵。
官方示例
原數據 特徵數爲64維(64個特徵)
>>> from sklearn.datasets import load_digits
>>> from sklearn.feature_selection import SelectPercentile, chi2
>>> X, y = load_digits(return_X_y=True)
>>> X.shape
(1797, 64)
以chi2方法
爲過濾器,篩選出分數在前10%的特徵,參數percentile=10
可以看出選出了7個特徵(降至7維)
>>> X_new = SelectPercentile(chi2, percentile=10).fit_transform(X, y)
>>> X_new.shape
(1797, 7)
更改參數爲percentile=20
,保留前20%
可以看出選出了13個特徵(降至13維)
>>> X_new = SelectPercentile(chi2, percentile=20).fit_transform(X, y)
>>> X_new.shape
(1797, 13)