VarianceThreshold#
- class sklearn.feature_selection.VarianceThreshold(threshold=0.0)[source]#
移除所有低方差特征的特征选择器。
此特征选择算法仅查看特征 (X),而不查看所需输出 (y),因此可用于无监督学习。
在用户指南中阅读更多内容。
- 参数:
- thresholdfloat, default=0
训练集方差低于此阈值的特征将被移除。默认值是保留所有非零方差的特征,即移除在所有样本中具有相同值的特征。
- 属性:
另请参阅
SelectFromModel基于重要性权重选择特征的元转换器。
SelectPercentile根据最高分数的百分位数选择特征。
SequentialFeatureSelector执行顺序特征选择的转换器。
注意事项
允许输入中包含 NaN。如果 X 中没有特征符合方差阈值,则会引发 ValueError。
示例
以下数据集具有整数特征,其中两个在每个样本中都相同。这些特征将使用默认的阈值设置被移除。
>>> from sklearn.feature_selection import VarianceThreshold >>> X = [[0, 2, 0, 3], [0, 1, 4, 3], [0, 1, 1, 3]] >>> selector = VarianceThreshold() >>> selector.fit_transform(X) array([[2, 0], [1, 4], [1, 1]])
- fit(X, y=None)[source]#
从 X 中学习经验方差。
- 参数:
- X{array-like, sparse matrix}, shape (n_samples, n_features)
用于计算方差的数据,其中
n_samples是样本数,n_features是特征数。- yany, default=None
被忽略。此参数仅用于与 sklearn.pipeline.Pipeline 兼容。
- 返回:
- 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]#
获取此对象的元数据路由。
请查阅 用户指南,了解路由机制如何工作。
- 返回:
- routingMetadataRequest
封装路由信息的
MetadataRequest。
- 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移除的特征位置插入了零列。
- 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
估计器实例。