RFECV#
- class sklearn.feature_selection.RFECV(estimator, *, step=1, min_features_to_select=1, cv=None, scoring=None, verbose=0, n_jobs=None, importance_getter='auto')[source]#
具有交叉验证的递归特征消除以选择特征。
通过在不同的交叉验证拆分(由
cv参数提供)上拟合一个RFE选择器,自动调整选择的特征数量。每个RFE选择器的性能使用scoring对不同数量的选定特征进行评估并聚合在一起。最后,分数在折叠之间取平均值,选择的特征数量被设置为使交叉验证分数最大化的特征数量。参见词汇表条目 交叉验证估计器。
在 用户指南 中阅读更多内容。
- 参数:
- estimator
Estimator实例 一个有监督学习估计器,具有
fit方法,并通过coef_属性或feature_importances_属性提供特征重要性信息。- stepint or float, default=1
如果大于或等于1,则
step对应于每次迭代要移除的特征数量(整数)。如果在 (0.0, 1.0) 范围内,则step对应于每次迭代要移除的特征百分比(向下取整)。请注意,最后一次迭代可能会移除少于step个特征,以便达到min_features_to_select。- min_features_to_selectint, default=1
要选择的最小特征数量。即使原始特征计数与
min_features_to_select之间的差值不能被step整除,也将始终对这个数量的特征进行评分。0.20 版本新增。
- cvint, cross-validation generator or an iterable, default=None
确定交叉验证拆分策略。cv 的可能输入包括
None,使用默认的 5 折交叉验证,
整数,指定折数。
一个可迭代对象,产生索引数组形式的 (训练集, 测试集) 拆分。
对于整数/None输入,如果
y是二元或多类别,则使用StratifiedKFold。如果估计器不是分类器,或者y既不是二元也不是多类别,则使用KFold。有关此处可使用的各种交叉验证策略,请参阅 用户指南。
版本 0.22 中的变化:
cv的默认值 None 从 3 折改为 5 折。- scoringstr 或可调用对象,默认=None
用于评估
RFE选择器性能的评分方法。选项str: 有关选项,请参阅 String name scorers。
callable: 带有签名
scorer(estimator, X, y)的可调用评分器对象(例如函数)。有关详细信息,请参阅 Callable scorers。None:使用estimator的 默认评估标准。
- verboseint, default=0
控制输出的详细程度。
- n_jobsint or None, default=None
在跨折叠拟合时并行运行的核心数。
None表示 1,除非在joblib.parallel_backend上下文中使用。-1表示使用所有处理器。有关更多详细信息,请参阅 词汇表。版本 0.18 新增。
- importance_getterstr or callable, default=’auto’
如果为 'auto',则使用估计器的
coef_或feature_importances_属性中的特征重要性。还接受一个字符串,指定用于提取特征重要性的属性名称/路径。例如,对于
TransformedTargetRegressor,可以指定regressor_.coef_;对于名为clf的最后一步的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实例 用于选择特征的已拟合估计器。
- cv_results_dict of ndarrays
所有数组(字典的值)都按使用的特征数量升序排序(即,数组的第一个元素表示使用最少特征的模型,而最后一个元素表示使用所有可用特征的模型)。
1.0 版本新增。
此字典包含以下键
- split(k)_test_scorendarray of shape (n_subsets_of_features,)
第 k 折交叉验证分数。
- mean_test_scorendarray of shape (n_subsets_of_features,)
折叠分数的平均值。
- std_test_scorendarray of shape (n_subsets_of_features,)
折叠分数的标准差。
- n_featuresndarray of shape (n_subsets_of_features,)
每一步使用的特征数量。
1.5 版本新增。
- split(k)_rankingndarray of shape (n_subsets_of_features,)
第 k 折交叉验证排名。选定(即估计的最佳)特征被分配排名 1。参见 Recursive feature elimination with cross-validation 中的插图。
在版本 1.7 中新增。
- split(k)_supportndarray of shape (n_subsets_of_features,)
第 k 折交叉验证支持。支持是选定特征的掩码。
在版本 1.7 中新增。
- n_features_int
通过交叉验证选择的特征数量。
- n_features_in_int
在 fit 期间看到的特征数。仅当底层估计器在拟合时暴露此属性时才定义。
0.24 版本新增。
- feature_names_in_shape 为 (
n_features_in_,) 的 ndarray 在 fit 期间看到的特征名称。仅当
X具有全部为字符串的特征名称时才定义。1.0 版本新增。
- ranking_narray of shape (n_features,)
特征排名,其中
ranking_[i]对应于第 i 个特征的排名位置。选定(即估计的最佳)特征被分配排名 1。- support_ndarray of shape (n_features,)
选定特征的掩码。
另请参阅
RFE递归特征消除。
注意事项
cv_results_中所有值的大小等于ceil((n_features - min_features_to_select) / step) + 1,其中 step 是每次迭代移除的特征数量。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 RFECV >>> from sklearn.svm import SVR >>> X, y = make_friedman1(n_samples=50, n_features=10, random_state=0) >>> estimator = SVR(kernel="linear") >>> selector = RFECV(estimator, step=1, cv=5) >>> 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])
有关使用 RFECV 选择特征以训练
LogisticRegression的详细示例,请参阅 Recursive feature elimination with cross-validation。- 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, **params)[source]#
拟合 RFE 模型并自动调整选定特征的数量。
- 参数:
- Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}
训练向量,其中
n_samples是样本数量,n_features是特征总数。- yarray-like of shape (n_samples,)
目标值(分类的整数,回归的实数)。
- **paramsstr -> object 字典
传递给估计器、评分器和 CV 分割器的
fit方法的参数。版本 1.6 中新增: 仅当
enable_metadata_routing=True时可用,可通过使用sklearn.set_config(enable_metadata_routing=True)进行设置。有关详细信息,请参阅元数据路由用户指南。
- 返回:
- 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]
在特征被
transform移除的位置插入零列的X。
- 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]#
使用给定测试数据和标签上的
scoring选项进行评分。- 参数:
- Xshape 为 (n_samples, n_features) 的 array-like
测试样本。
- yarray-like of shape (n_samples,)
X 的真实标签。
- **score_paramsdict
要传递给底层评分器
score方法的参数。版本 1.6 中新增: 仅当
enable_metadata_routing=True时可用,可通过使用sklearn.set_config(enable_metadata_routing=True)进行设置。有关详细信息,请参阅元数据路由用户指南。
- 返回:
- scorefloat
根据
scoring定义的 self.predict(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
估计器实例。