sklearn.feature_selection.RFE?
class sklearn.feature_selection.RFE(estimator, *, n_features_to_select=None, step=1, verbose=0)
具有遞歸特征消除的特征排序。
給定將權重分配給特征(例如線性模型的系數)的外部估計器,遞歸特征消除(RFE)的目標是通過遞歸考慮越來越少的特征集來選擇特征。首先,對估計器進行初始特征集訓練,并通過coef_
屬性或 feature_importances_
屬性獲得每個特征的重要性。然后,從當前特征集中刪除最不重要的特征。在經過修剪的集合上遞歸地重復這個過程,直到達到需要選擇的特征數量。
在用戶指南中閱讀更多內容。
參數 | 說明 |
---|---|
estimator | object 一種監督學習估計器,其 fit 方法通過coef_ 屬性或feature_importances_ 屬性提供有關特征重要性的信息。 |
n_features_to_select | int or None (default=None) 要選擇的特征數量。如果為 None ,則選擇一半特征。 |
step | int or float, optional (default=1) 如果大于或等于1,則 step 對應于每次迭代要刪除的特征個數(整數)。如果在(0.0,1.0)之內,則step 對應于每次迭代要刪除的特征的百分比(向下舍入)。 |
verbose | int, (default=0) 控制輸出的詳細程度。 |
屬性 | 說明 |
---|---|
n_features_ | int 所選特征的數量。 |
support_ | array of shape [n_features] 所選特征的掩碼。 |
ranking_ | array of shape [n_features] 特征排序,使ranking_[i]對應第i個特征的排序位置。選擇的(即估計的最佳)特征被排在第1位。 |
estimator_ | object 擬合簡化后數據集的外部估算器。 |
另見
通過內置的交叉驗證選擇最佳數量的特征來消除遞歸特征
注
如果基礎估算器也可以輸入,則允許NaN / Inf。
參考
1 Guyon, I., Weston, J., Barnhill, S., & Vapnik, V., “Gene selection for cancer classification using support vector machines”, Mach. Learn., 46(1-3), 389–422, 2002.
示例
以下示例顯示如何在Friedman#1數據集中檢索5個最有用的特征。
>>> from sklearn.datasets import make_friedman1
>>> from sklearn.feature_selection import RFE
>>> from sklearn.svm import SVR
>>> X, y = make_friedman1(n_samples=50, n_features=10, random_state=0)
>>> estimator = SVR(kernel="linear")
>>> selector = RFE(estimator, n_features_to_select=5, step=1)
>>> selector = selector.fit(X, y)
>>> selector.support_
array([ True, True, True, True, True, False, False, False, False,
False])
>>> selector.ranking_
array([1, 1, 1, 1, 1, 6, 4, 3, 2, 5])
方法
方法 | 說明 |
---|---|
decision_function (X) |
計算X 的決策函數。 |
fit (X, y) |
對所選特征擬合RFE模型和底層估計器。 |
fit_transform (X[, y]) |
擬合數據并對其進行轉換。 |
get_params ([deep]) |
獲取此估計器的參數。 |
get_support ([indices]) |
獲取所選特征的掩碼或整數索引。 |
inverse_transform (X) |
反向轉換操作 |
predict (X) |
將X簡化為選定的特征,然后使用基估計器進行預測。 |
predict_log_proba (X) |
預測X的類對數概率。 |
predict_proba (X) |
預測X的類概率。 |
score (X, y) |
將X簡化為選擇的特征,然后返回基估計器的分數。 |
set_params (**params) |
設置此估算器的參數。 |
transform (X) |
將X縮小為選定的特征。 |
__init__(estimator, *, n_features_to_select=None, step=1, verbose=0)
初始化self,參見help(type(self))獲取更多信息。
decision_function(X)
計算X
的決策函數。
參數 | 說明 |
---|---|
X | {array-like or sparse matrix} of shape (n_samples, n_features) 輸入樣本。在內部,如果將稀疏矩陣提供給 csr_matrix ,它將轉換為 dtype=np.float32 。 |
返回值 | 說明 |
---|---|
score | array, shape = [n_samples, n_classes] or [n_samples] 輸入樣本的決策函數。類的順序與屬性classes_中的順序相對應。回歸和二分類產生一個形狀為[n_samples]的數組。 |
fit(X, y)
對所選特征擬合RFE模型和底層估計器。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) 訓練輸入樣本。 |
y | array-like of shape (n_samples,) 目標值。 |
返回值 | 說明 |
---|---|
self | object |
fit_transform(X, y=None, **fit_params)
擬合數據,然后對其進行轉換。
使用可選參數fit_params將轉換器擬合到X和y,并返回X的轉換值。
參數 | 說明 |
---|---|
X | {array-like, sparse matrix, dataframe} of shape (n_samples, n_features) |
y | ndarray of shape (n_samples,), default=None 目標值 |
**fit_params | dict 其他擬合參數。 |
返回值 | 說明 |
---|---|
X_new | ndarray array of shape (n_samples, n_features_new) 轉換后的數組。 |
get_params(deep=True)
獲取此估計器的參數。
參數 | 說明 |
---|---|
deep | bool, default=True 如果為True,則將返回此估算器和所包含子對象的參數。 |
返回值 | 說明 |
---|---|
params | mapping of string to any 參數名稱映射到其值。 |
get_support(indices=False)
獲取所選特征的掩碼或整數索引。
參數 | 說明 |
---|---|
indices | boolean (default False) 如果為True,則返回值將是一個整數數組,而不是布爾掩碼。 |
返回值 | 說明 |
---|---|
support | array 從特征向量中選擇保留特征的索引。如果 indices 為False,則為形狀為[#輸入特征]的布爾數組,其中元素為True時(如果已選擇其對應的特征進行保留)。如果indices 為True,則這是一個形狀為[#輸出特征]的整數數組,其值是輸入特征向量的索引。 |
inverse_transform(X)
反向轉換操作。
參數 | 說明 |
---|---|
X | array of shape [n_samples, n_selected_features] 輸入樣本。 |
返回值 | 說明 |
---|---|
X_r | array of shape [n_samples, n_original_features]X 中插入的列名為零的特征將被transform 刪除。 |
predict(X)
將X簡化為選定的特征,然后使用基估計器進行預測。
參數 | 說明 |
---|---|
X | array of shape [n_samples, n_features] 輸入樣本。 |
返回值 | 說明 |
---|---|
y | array of shape [n_samples] 預測目標值。 |
predict_log_proba(X )
預測X的類對數概率。
參數 | 說明 |
---|---|
X | array of shape [n_samples, n_features] 輸入樣本。 |
返回值 | 說明 |
---|---|
y | array of shape [n_samples] 輸入樣本的類對數概率。類的順序與屬性classes_中的順序相對應。 |
predict_proba(X )
參數 | 說明 |
---|---|
X | {array-like or sparse matrix} of shape (n_samples, n_features) 輸入樣本。在內部,如果將稀疏矩陣提供給 csr_matrix ,它將轉換為 dtype=np.float32 。 |
返回值 | 說明 |
---|---|
p | array of shape (n_samples, n_classes) 輸入樣本的分類概率。類的順序與屬性classes_中的順序相對應。 |
score(X,y )
將X簡化為選擇的特征,然后返回基估計器的分數。
參數 | 說明 |
---|---|
X | array of shape [n_samples, n_features] 輸入樣本。 |
y | array of shape [n_samples] 目標值。 |
set_params(**params)
設置此估算器的參數。
該方法適用于簡單的估計器以及嵌套對象(例如管道)。后者具有<component>__<parameter>
形式的參數, 以便可以更新嵌套對象的每個組件。
參數 | 說明 |
---|---|
**params | dict 估計器參數。 |
返回值 | 說明 |
---|---|
self | object 估計器實例。 |
transform(X)
將X縮小為選定的特征。
參數 | 說明 |
---|---|
X | array of shape [n_samples, n_features] 輸入樣本。 |
返回值 | 說明 |
---|---|
X_r | array of shape [n_samples, n_selected_features] 僅具有所選特征的輸入樣本。 |