SelectFromModel#

class sklearn.feature_selection.SelectFromModel(estimator, *, threshold=None, prefit=False, norm_order=1, max_features=None, importance_getter='auto')[源代码]#

元转换器,用于根据重要性权重选择特征。

Added in version 0.17.

阅读更多的 User Guide .

参数:
estimator对象

构建Transformer的基本估计器。这可以既合适(如果 prefit 被设置为True)或非匹配估计量。估算者应该有一个 feature_importances_coef_ 装配后的属性。否则 importance_getter 应该使用参数。

threshold字符串或float,默认=无

用于特征选择的阈值。保留绝对重要性值大于或等于的特征,而丢弃其他特征。如果“中位数”(分别“意思”),那么 threshold 值是中位数(分别特征重要性的平均值)。比例因子(例如,也可以使用“1.25* means”)。如果无并且如果估计器的参数罚分被设置为l1(无论是显式还是隐式(例如Lasso)),则使用的阈值是1 e-5。否则,默认使用“意思”。

prefit布尔,默认=假

预调整模型是否预计直接传递到构造函数中。如果 True , estimator 必须是一个拟合估计量。如果 False , estimator 通过调用 fitpartial_fit ,分别。

norm_order非零int,inf,-inf,默认值=1

用于过滤下面系数的载体的规范的顺序 threshold 的情况下 coef_ 估计器的属性是维度2。

max_featuresint,callable,default=None

要选择的最大特征数。

  • 如果是一个整数,那么它指定允许的最大要素数。

  • 如果是可调用的,则它指定如何使用的输出计算允许的最大要素数 max_features(X) .

  • 如果 None ,然后所有功能都被保留。

仅根据 max_features ,设置 threshold=-np.inf .

Added in version 0.20.

在 1.1 版本发生变更: max_features 接受可调用。

importance_getter字符串或可调用,默认=' Auto '

如果为“自动”,则通过 coef_ 属性或 feature_importances_ 估计器的属性。

还接受指定用于提取要素重要性的属性名称/路径的字符串(使用实现 attrgetter ).比如给 regressor_.coef_ 的情况下 TransformedTargetRegressornamed_steps.clf.feature_importances_ 的情况下 Pipeline 其最后一步命名为 clf .

如果 callable ,覆盖默认功能重要性获取器。调用对象与匹配的估计器一起传递,并且它应该返回每个特征的重要性。

Added in version 0.24.

属性:
estimator_估计器

构建Transformer的基本估计器。此属性仅存在于 fit 已被呼叫。

  • 如果 prefit=True ,它是《 estimator .

  • 如果 prefit=False ,它是 estimator 并适合传递给的数据 fitpartial_fit .

n_features_in_int

期间看到的功能数量 fit .

max_features_int

期间计算的最大要素数 fit .仅当 max_featuresNone .

  • 如果 max_features 是一 int 那么 max_features_ = max_features .

  • 如果 max_features 是一个可调用的,那么 max_features_ = max_features(X) .

Added in version 1.1.

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.

threshold_浮子

用于特征选择的阈值。

参见

RFE

基于重要性权重的递进特征消除。

RFECV

通过内置交叉验证选择最佳数量的特征来实现渐进特征消除。

SequentialFeatureSelector

基于顺序交叉验证的特征选择。不依赖于重要性权重。

注意到

如果基础估计器也允许NaN/Inf,则允许输入中的NaN/Inf。

示例

>>> from sklearn.feature_selection import SelectFromModel
>>> from sklearn.linear_model import LogisticRegression
>>> X = [[ 0.87, -1.34,  0.31 ],
...      [-2.79, -0.02, -0.85 ],
...      [-1.34, -0.48, -2.55 ],
...      [ 1.92,  1.48,  0.65 ]]
>>> y = [0, 1, 0, 1]
>>> selector = SelectFromModel(estimator=LogisticRegression()).fit(X, y)
>>> selector.estimator_.coef_
array([[-0.3252...,  0.8345...,  0.4976...]])
>>> selector.threshold_
np.float64(0.55249...)
>>> selector.get_support()
array([False,  True, False])
>>> selector.transform(X)
array([[-1.34],
       [-0.02],
       [-0.48],
       [ 1.48]])

使用可调用来创建一个可以使用不超过一半输入功能的选择器。

>>> def half_callable(X):
...     return round(len(X[0]) / 2)
>>> half_selector = SelectFromModel(estimator=LogisticRegression(),
...                                 max_features=half_callable)
>>> _ = half_selector.fit(X, y)
>>> half_selector.max_features_
2
fit(X, y=None, **fit_params)[源代码]#

适应SelectFromModel元转换器。

参数:
X形状类似阵列(n_samples,n_features)

训练输入样本。

y形状类似数组(n_samples,),默认=无

目标值(与分类中的类别对应的整数,回归中的真实数字)。

**fit_paramsdict
  • 如果 enable_metadata_routing=False (默认):参数直接传递给 fit 次估计器的方法。如果他们被忽视 prefit=True .

  • 如果 enable_metadata_routing=True :参数安全路由到 fit 次估计器的方法。如果他们被忽视 prefit=True .

在 1.4 版本发生变更: 看到 Metadata Routing User Guide 了解更多详细信息。

返回:
self对象

拟合估计量。

fit_transform(X, y=None, **fit_params)[源代码]#

适应数据,然后对其进行转换。

适合变压器 Xy 具有可选参数 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_featuresNone 那么 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.4.

返回:
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 .

partial_fit(X, y=None, **partial_fit_params)[源代码]#

仅适合一次SelectFromModel元转换器。

参数:
X形状类似阵列(n_samples,n_features)

训练输入样本。

y形状类似数组(n_samples,),默认=无

目标值(与分类中的类别对应的整数,回归中的真实数字)。

**partial_fit_paramsdict
  • If enable_metadata_routing=False (default): Parameters directly passed to the partial_fit method of the sub-estimator.

  • 如果 enable_metadata_routing=True :传递给 partial_fit 次估计器的方法。如果他们被忽视 prefit=True .

在 1.4 版本发生变更: **partial_fit_params 如果 enable_metadata_routing=True 设置为 set_config ,这允许混叠。

看到 Metadata Routing User Guide 了解更多详细信息。

返回:
self对象

拟合估计量。

set_output(*, transform=None)[源代码]#

设置输出容器。

看到 介绍 set_output API 了解如何使用API的示例。

参数:
transform{“默认”,“pandas”,“polars”},默认=无

配置输出 transformfit_transform .

  • "default" :Transformer的默认输出格式

  • "pandas" :DataFrame输出

  • "polars" :两极输出

  • None :转换配置不变

Added in version 1.4: "polars" 添加了选项。

返回:
self估计器实例

估计实例。

set_params(**params)[源代码]#

设置此估计器的参数。

该方法适用于简单估计器以及嵌套对象(例如 Pipeline ).后者具有以下形式的参数 <component>__<parameter> 以便可以更新嵌套对象的每个组件。

参数:
**paramsdict

估计参数。

返回:
self估计器实例

估计实例。

transform(X)[源代码]#

Reduce X to the selected features.

参数:
X形状数组 [n_samples, n_features]

输入样本。

返回:
X_r形状数组 [n_samples, n_selected_features]

仅具有选定特征的输入样本。