RFE#
- class sklearn.feature_selection.RFE(estimator, *, n_features_to_select=None, step=1, verbose=0, importance_getter='auto')[source]#
具有递归特征消除的特征排名。
给定一个为特征分配权重的外部估计器(例如,线性模型的系数),递归特征消除(RFE)的目标是通过递归地考虑越来越小的特征集来选择特征。首先,估计器在初始特征集上进行训练,并通过任何特定属性或可调用对象获取每个特征的重要性。然后,从当前特征集中修剪掉最不重要的特征。该过程在修剪后的集合上递归重复,直到最终达到所需的特征选择数量。
在用户指南中阅读更多内容。
- 参数:
- estimator
Estimator实例 一个具有
fit方法的监督学习估计器,该方法提供有关特征重要性的信息(例如coef_,feature_importances_)。- n_features_to_selectint or float, default=None
要选择的特征数量。如果为
None,则选择一半的特征。如果是整数,则参数是要选择的特征的绝对数量。如果是介于 0 和 1 之间的浮点数,则是要选择的特征的比例。版本 0.24 中的变化: 添加了用于表示比例的浮点值。
- stepint or float, default=1
如果大于或等于 1,则
step对应于每次迭代中要移除的特征的(整数)数量。如果介于 (0.0, 1.0) 之间,则step对应于每次迭代中要移除的特征的百分比(向下取整)。- verboseint, default=0
控制输出的详细程度。
- importance_getterstr or callable, default=’auto’
如果为 'auto',则使用估计器的
coef_或feature_importances_属性来获取特征重要性。也接受一个字符串,指定用于提取特征重要性的属性名称/路径(通过
attrgetter实现)。例如,对于TransformedTargetRegressor,提供regressor_.coef_;对于名为clf的最后一个步骤的~sklearn.pipeline.Pipeline类,提供named_steps.clf.feature_importances_。If
callable, overrides the default feature importance getter. The callable is passed with the fitted estimator and it should return importance for each feature.0.24 版本新增。
- estimator
- 属性:
classes_形状为 (n_classes,) 的 ndarray当
estimator是分类器时可用的类别标签。- estimator_
Estimator实例 用于选择特征的已拟合估计器。
- n_features_int
所选特征的数量。
- n_features_in_int
在 fit 期间看到的特征数。仅当底层估计器在拟合时暴露此属性时才定义。
0.24 版本新增。
- feature_names_in_shape 为 (
n_features_in_,) 的 ndarray 在 fit 期间看到的特征名称。仅当
X具有全部为字符串的特征名称时才定义。1.0 版本新增。
- ranking_ndarray of shape (n_features,)
特征排名,其中
ranking_[i]对应于第 i 个特征的排名位置。选定的(即估计的最佳)特征被分配排名 1。- support_ndarray of shape (n_features,)
选定特征的掩码。
另请参阅
RFECV具有内置交叉验证选择最佳特征数量的递归特征消除。
SelectFromModel基于重要性权重阈值的特征选择。
SequentialFeatureSelector基于顺序交叉验证的特征选择。不依赖于重要性权重。
注意事项
Allows NaN/Inf in the input if the underlying estimator does as well.
References
[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)[source]#
计算
X的决策函数。- 参数:
- X{array-like or sparse matrix} of shape (n_samples, n_features)
输入样本。在内部,它将转换为
dtype=np.float32,如果提供了稀疏矩阵,则转换为稀疏的csr_matrix。
- 返回:
- scorearray, shape = [n_samples, n_classes] or [n_samples]
输入样本的决策函数。类别的顺序对应于属性 classes_ 中的顺序。回归和二元分类产生形状为 [n_samples] 的数组。
- fit(X, y, **fit_params)[source]#
拟合 RFE 模型,然后使用所选特征拟合底层估计器。
- 参数:
- Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}
训练输入样本。
- yarray-like of shape (n_samples,)
目标值。
- **fit_paramsdict
如果
enable_metadata_routing=False(默认值):参数直接传递给底层估计器的fit方法。如果
enable_metadata_routing=True:参数安全地路由到底层估计器的fit方法。
版本 1.6 中的变化: 有关更多详细信息,请参阅 元数据路由用户指南。
- 返回:
- selfobject
拟合的估计器。
- fit_transform(X, y=None, **fit_params)[source]#
拟合数据,然后对其进行转换。
使用可选参数
fit_params将转换器拟合到X和y,并返回X的转换版本。- 参数:
- Xshape 为 (n_samples, n_features) 的 array-like
输入样本。
- y形状为 (n_samples,) 或 (n_samples, n_outputs) 的类数组对象,默认=None
目标值(对于无监督转换,为 None)。
- **fit_paramsdict
额外的拟合参数。仅当估计器在其
fit方法中接受额外的参数时才传递。
- 返回:
- X_newndarray array of shape (n_samples, n_features_new)
转换后的数组。
- get_feature_names_out(input_features=None)[source]#
根据所选特征屏蔽特征名称。
- 参数:
- input_featuresarray-like of str or None, default=None
输入特征。
如果
input_features为None,则使用feature_names_in_作为输入特征名称。如果feature_names_in_未定义,则生成以下输入特征名称:["x0", "x1", ..., "x(n_features_in_ - 1)"]。如果
input_features是 array-like,则如果定义了feature_names_in_,input_features必须与feature_names_in_匹配。
- 返回:
- feature_names_outstr 对象的 ndarray
转换后的特征名称。
- get_metadata_routing()[source]#
获取此对象的元数据路由。
请查阅 用户指南,了解路由机制如何工作。
版本 1.6 中新增。
- 返回:
- routingMetadataRouter
封装路由信息的
MetadataRouter。
- get_params(deep=True)[source]#
获取此估计器的参数。
- 参数:
- deepbool, default=True
如果为 True,将返回此估计器以及包含的子对象(如果它们是估计器)的参数。
- 返回:
- paramsdict
参数名称映射到其值。
- get_support(indices=False)[source]#
获取所选特征的掩码或整数索引。
- 参数:
- indicesbool, default=False
如果为 True,返回值将是一个整数数组,而不是布尔掩码。
- 返回:
- supportarray
从特征向量中选择保留特征的索引。如果
indices为 False,则这是一个形状为 [# input features] 的布尔数组,其中元素为 True 当且仅当其对应的特征被选中保留。如果indices为 True,则这是一个形状为 [# output features] 的整数数组,其值是输入特征向量中的索引。
- inverse_transform(X)[source]#
反转转换操作。
- 参数:
- Xarray of shape [n_samples, n_selected_features]
输入样本。
- 返回:
- X_originalarray of shape [n_samples, n_original_features]
将
X中的列插入零,这些列是transform移除的特征所在的位置。
- predict(X, **predict_params)[source]#
将 X 降维到所选特征并使用估计器进行预测。
- 参数:
- Xarray of shape [n_samples, n_features]
输入样本。
- **predict_paramsdict
要路由到底层估计器的
predict方法的参数。版本 1.6 中新增: 仅当
enable_metadata_routing=True时可用,可通过使用sklearn.set_config(enable_metadata_routing=True)进行设置。有关详细信息,请参阅元数据路由用户指南。
- 返回:
- yarray of shape [n_samples]
预测的目标值。
- predict_log_proba(X)[source]#
预测 X 的类别对数概率。
- 参数:
- Xarray of shape [n_samples, n_features]
输入样本。
- 返回:
- parray of shape (n_samples, n_classes)
输入样本的类别对数概率。类的顺序对应于属性 classes_ 中的顺序。
- predict_proba(X)[source]#
预测 X 的类别概率。
- 参数:
- X{array-like or sparse matrix} of shape (n_samples, n_features)
输入样本。在内部,它将转换为
dtype=np.float32,如果提供了稀疏矩阵,则转换为稀疏的csr_matrix。
- 返回:
- parray of shape (n_samples, n_classes)
输入样本的类别概率。类的顺序对应于属性 classes_ 中的顺序。
- score(X, y, **score_params)[source]#
将 X 降维到所选特征并返回估计器的得分。
- 参数:
- Xarray of shape [n_samples, n_features]
输入样本。
- yarray of shape [n_samples]
目标值。
- **score_paramsdict
如果
enable_metadata_routing=False(默认值):参数直接传递给底层估计器的score方法。如果
enable_metadata_routing=True:参数安全地路由到底层估计器的score方法。
1.0 版本新增。
版本 1.6 中的变化: 有关更多详细信息,请参阅 元数据路由用户指南。
- 返回:
- scorefloat
使用
rfe.transform(X)返回的所选特征和y计算的底层基本估计器的得分。
- set_output(*, transform=None)[source]#
设置输出容器。
有关如何使用 API 的示例,请参阅引入 set_output API。
- 参数:
- transform{“default”, “pandas”, “polars”}, default=None
配置
transform和fit_transform的输出。"default": 转换器的默认输出格式"pandas": DataFrame 输出"polars": Polars 输出None: 转换配置保持不变
1.4 版本新增: 添加了
"polars"选项。
- 返回:
- selfestimator instance
估计器实例。