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')[源代码]#
通过交叉验证来选择特征的渐进特征消除。
所选要素的数量通过匹配自动调整
RFE
不同交叉验证拆分上的选择器(由cv
参数)。的性能RFE
选择器的评估使用scorer
针对不同数量的选定特征并聚合在一起。最后,将分数跨折叠取平均,并将所选特征的数量设置为使交叉验证分数最大化的特征数量。请参阅术语表条目 cross-validation estimator .阅读更多的 User Guide .
- 参数:
- estimator :
Estimator
例如估计实例 具有监督学习估计器
fit
通过coef_
属性或通过feature_importances_
属性- stepint或float,默认=1
如果大于或等于1,则
step
对应于每次迭代要删除的特征的(整)数量。如果在(0.0,1.0)内,那么step
对应于每次迭代时要删除的功能的百分比(向下四舍五入)。请注意,最后一次迭代可能会删除少于step
为了达到目的min_features_to_select
.- min_features_to_selectint,默认=1
要选择的最小功能数量。即使原始特征计数和
min_features_to_select
不能被step
.Added in version 0.20.
- cvint,交叉验证生成器或可迭代对象,默认=无
确定交叉验证拆分策略。简历的可能输入包括:
无,若要使用默认的5重交叉验证,
integer,用于指定折叠次数。
可迭代产出(训练、测试)分裂为索引数组。
对于integer/Non-输入,如果
y
是二进制或多类,StratifiedKFold
采用了如果估计器不是分类器或者如果y
既不是二元也不是多元的,KFold
采用了指 User Guide 这里可以使用的各种交叉验证策略。
在 0.22 版本发生变更:
cv
默认值无从3倍更改为5倍。- scoring字符串,可调用或无,默认=无
字符串(请参阅 的 scoring 参数:定义模型评估规则 )或具有签名的记分器可调用对象/函数
scorer(estimator, X, y)
.- verboseint,默认=0
控制输出的详细程度。
- n_jobsint或无,默认=无
穿过折痕时平行运行的芯数。
None
意思是1,除非在a中joblib.parallel_backend
上下文-1
意味着使用所有处理器。看到 Glossary 了解更多详细信息。Added in version 0.18.
- importance_getter字符串或可调用,默认=' Auto '
如果为“自动”,则通过
coef_
或feature_importances_
估计量的属性。还接受指定用于提取特征重要性的属性名称/路径的字符串。比如给
regressor_.coef_
的情况下TransformedTargetRegressor
或named_steps.clf.feature_importances_
的情况下Pipeline
其最后一步命名为clf
.如果
callable
,覆盖默认功能重要性获取器。调用对象与匹配的估计器一起传递,并且它应该返回每个特征的重要性。Added in version 0.24.
- estimator :
- 属性:
classes_
形状的nd数组(n_classes,)类别标签可用时
estimator
是分类器。- estimator_ :
Estimator
例如估计实例 用于选择特征的匹配估计量。
- cv_results_恩德雷法令
所有数组(字典的值)都按照所使用的要素数量按自升顺序排序(即,数组的第一个元素表示使用最少数量特征的模型,而最后一个元素表示使用所有可用特征的模型)。
Added in version 1.0.
此字典包含以下键:
- split(k)_Test_score形状的nd数组(n_subset_of_features,)
交叉验证分数跨越第(k)倍。
- mean_test_score形状的nd数组(n_subset_of_features,)
折痕上得分的平均值。
- std_test_score形状的nd数组(n_subset_of_features,)
折痕分数的标准差。
- n_features形状的nd数组(n_subset_of_features,)
每个步骤使用的功能数量。
Added in version 1.5.
- n_features_int
具有交叉验证的选定功能的数量。
- n_features_in_int
期间看到的功能数量 fit .仅在基础估计器在适合时暴露此类属性时才定义。
Added in version 0.24.
- feature_names_in_ :nd形状数组 (
n_features_in_
,)nd数组形状( Names of features seen during fit. Defined only when
X
has feature names that are all strings.Added in version 1.0.
- ranking_n数组形状(n_features,)
功能排名,例如
ranking_[i]
对应于第i个要素的排名位置。选择(即,估计的最佳)特征被分配为排名1。- support_形状的nd数组(n_features,)
选定特征的面具。
参见
RFE
循环特征消除。
注意到
中所有值的大小
cv_results_
等于ceil((n_features - min_features_to_select) / step) + 1
,其中步骤是每次迭代时删除的特征数量。如果基础估计器也允许NaN/Inf,则允许输入中的NaN/Inf。
引用
[1]盖伊,I.,Weston,J.,南卡罗来纳州巴恩希尔,& Vapnik,V.,“使用支持载体机进行癌症分类的基因选择”,马赫。学习。,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])
- decision_function(X)[源代码]#
计算的决策函数
X
.- 参数:
- X形状(n_样本,n_特征)的{类阵列或稀疏矩阵}
输入样本。在内部,它将被转换为
dtype=np.float32
并且如果将稀疏矩阵提供给稀疏矩阵csr_matrix
.
- 返回:
- score数组,形状= [n_samples, n_classes] 或 [n_samples]
输入样本的决策函数。类的顺序与属性中的顺序相对应 classes_ .回归和二元分类产生一系列形状 [n_samples] .
- fit(X, y, *, groups=None, **params)[源代码]#
适应RFE模型并自动调整选定功能的数量。
- 参数:
- X形状(n_samples,n_features)的{类数组,稀疏矩阵}
训练载体,在哪里
n_samples
是样本数量和n_features
是功能的总数。- y形状类似阵列(n_samples,)
目标值(用于分类的积分,用于回归的积分)。
- groups形状类似数组(n_samples,)或无,默认=无
Group labels for the samples used while splitting the dataset into train/test set. Only used in conjunction with a "Group" cv instance (e.g.,
GroupKFold
).Added in version 0.20.
- **params字符串->对象的字典
参数传递给
fit
估计器、评分器和CV拆分器的方法。Added in version 1.6: 仅在以下情况下可用
enable_metadata_routing=True
,可以使用sklearn.set_config(enable_metadata_routing=True)
.看到 Metadata Routing User Guide 了解更多详细信息。
- 返回:
- self对象
拟合估计量。
- fit_transform(X, y=None, **fit_params)[源代码]#
适应数据,然后对其进行转换。
适合变压器
X
和y
具有可选参数fit_params
并返回的转换版本X
.- 参数:
- X形状类似阵列(n_samples,n_features)
输入样本。
- y形状为(n_samples,)或(n_samples,n_outputs)的阵列状, 默认值=无
目标值(无监督转换)。
- **fit_paramsdict
其他适合参数。
- 返回:
- X_newndray形状数组(n_samples,n_features_new)
变形的数组。
- get_feature_names_out(input_features=None)[源代码]#
Mask feature names according to selected features.
- 参数:
- input_features字符串或无的类数组,默认=无
输入功能。
如果
input_features
是None
那么feature_names_in_
在中用作功能名称。如果feature_names_in_
未定义,则生成以下输入要素名称:["x0", "x1", ..., "x(n_features_in_ - 1)"]
.如果
input_features
是一个类似阵列的,那么input_features
必须匹配feature_names_in_
如果feature_names_in_
是定义的。
- 返回:
- feature_names_out字符串对象的nd数组
转换的功能名称。
- get_metadata_routing()[源代码]#
获取此对象的元数据路由。
请检查 User Guide 关于路由机制如何工作。
Added in version 1.6.
- 返回:
- routingMetadataRouter
A
MetadataRouter
封装路由信息。
- get_params(deep=True)[源代码]#
获取此估计器的参数。
- 参数:
- deep布尔,默认=True
如果为True,将返回此估计量和包含的作为估计量的子对象的参数。
- 返回:
- paramsdict
参数名称映射到其值。
- get_support(indices=False)[源代码]#
获取所选要素的屏蔽或整指数。
- 参数:
- indices布尔,默认=假
如果为True,则返回值将是一个integer数组,而不是布尔屏蔽。
- 返回:
- support阵列
从特征载体中选择保留特征的索引。如果
indices
为假,这是形状的布尔数组 [# input features] ,其中只要选择其相应的特征进行保留,元素就为True。如果indices
是True,这是一个形状的整数组 [# output features] 其值是输入特征载体的索引。
- inverse_transform(X)[源代码]#
逆转转型操作。
- 参数:
- X形状数组 [n_samples, n_selected_features]
输入样本。
- 返回:
- X_r形状数组 [n_samples, n_original_features]
X
在将删除要素的位置插入零列transform
.
- predict(X, **predict_params)[源代码]#
将X减少到所选特征并使用估计器进行预测。
- 参数:
- X形状数组 [n_samples, n_features]
输入样本。
- **predict_paramsdict
要路由到
predict
基本估计者的方法。Added in version 1.6: 仅在以下情况下可用
enable_metadata_routing=True
,可以使用sklearn.set_config(enable_metadata_routing=True)
.看到 Metadata Routing User Guide 了解更多详细信息。
- 返回:
- y形状数组 [n_samples]
预测目标值。
- predict_log_proba(X)[源代码]#
预测X的类对数概率。
- 参数:
- X形状数组 [n_samples, n_features]
输入样本。
- 返回:
- p形状数组(n_samples,n_classes)
输入样本的类对数概率。类的顺序与属性中的顺序相对应 classes_ .
- predict_proba(X)[源代码]#
预测X的类概率。
- 参数:
- X形状(n_样本,n_特征)的{类阵列或稀疏矩阵}
输入样本。在内部,它将被转换为
dtype=np.float32
并且如果将稀疏矩阵提供给稀疏矩阵csr_matrix
.
- 返回:
- p形状数组(n_samples,n_classes)
输入样本的类概率。类的顺序与属性中的顺序相对应 classes_ .
- score(X, y, **score_params)[源代码]#
使用评分
scoring
给定测试数据和标签上的选项。- 参数:
- X形状类似阵列(n_samples,n_features)
测试样本。
- y形状类似阵列(n_samples,)
真正的标签X。
- **score_paramsdict
要传递给
score
潜在得分者的方法。Added in version 1.6: 仅在以下情况下可用
enable_metadata_routing=True
,可以使用sklearn.set_config(enable_metadata_routing=True)
.看到 Metadata Routing User Guide 了解更多详细信息。
- 返回:
- score浮子
自我评分。预测(X)w.rt. y定义为
scoring
.
- set_output(*, transform=None)[源代码]#
设置输出容器。
看到 介绍 set_output API 了解如何使用API的示例。
- 参数:
- transform{“默认”,“pandas”,“polars”},默认=无
配置输出
transform
和fit_transform
."default"
:Transformer的默认输出格式"pandas"
:DataFrame输出"polars"
:两极输出None
:转换配置不变
Added in version 1.4:
"polars"
添加了选项。
- 返回:
- self估计器实例
估计实例。