GaussianRandomProjection#

class sklearn.random_projection.GaussianRandomProjection(n_components='auto', *, eps=0.1, compute_inverse_components=False, random_state=None)[source]#

通过高斯随机投影降低维度。

随机矩阵的成分是从 N(0, 1 / n_components) 分布中抽取的。

用户指南 中阅读更多内容。

在版本 0.13 中添加。

参数:
n_componentsint 或 ‘auto’,默认值:‘auto’

目标投影空间的维度。

根据 Johnson-Lindenstrauss 引理中的样本数量和给定的界限,可以自动调整 n_components。在这种情况下,嵌入的质量由 eps 参数控制。

应该注意的是,Johnson-Lindenstrauss 引理可能对所需组件数量的估计非常保守,因为它不对数据集的结构做任何假设。

epsfloat,默认值:0.1

n_components 设置为 ‘auto’ 时,用于根据 Johnson-Lindenstrauss 引理控制嵌入质量的参数。该值应严格为正。

较小的值会导致更好的嵌入和目标投影空间中更高的维度(n_components)。

compute_inverse_componentsbool,默认值:False

通过在 fit 时计算组件的伪逆来学习反向变换。请注意,计算伪逆对于大型矩阵来说扩展性不佳。

random_stateint, RandomState instance or None, default=None

控制在 fit 时用于生成投影矩阵的伪随机数生成器。传递一个整数以获得跨多个函数调用的可重现输出。请参阅 Glossary

属性:
n_components_int

当 n_components=”auto” 时计算出的组件的实际数量。

components_ndarray of shape (n_components, n_features)

用于投影的随机矩阵。

inverse_components_ndarray,形状为 (n_features, n_components)

组件的伪逆,仅在 compute_inverse_components 为 True 时计算。

版本 1.1 中新增。

n_features_in_int

拟合 期间看到的特征数。

0.24 版本新增。

feature_names_in_shape 为 (n_features_in_,) 的 ndarray

fit 期间看到的特征名称。仅当 X 具有全部为字符串的特征名称时才定义。

1.0 版本新增。

另请参阅

SparseRandomProjection

通过稀疏随机投影降低维度。

示例

>>> import numpy as np
>>> from sklearn.random_projection import GaussianRandomProjection
>>> rng = np.random.RandomState(42)
>>> X = rng.rand(25, 3000)
>>> transformer = GaussianRandomProjection(random_state=rng)
>>> X_new = transformer.fit_transform(X)
>>> X_new.shape
(25, 2759)
fit(X, y=None)[source]#

生成稀疏随机投影矩阵。

参数:
X{ndarray, sparse matrix} of shape (n_samples, n_features)

训练集:仅使用形状来根据上述论文中引用的理论找到最佳随机矩阵维度。

y被忽略

Not used, present here for API consistency by convention.

返回:
selfobject

BaseRandomProjection 类实例。

fit_transform(X, y=None, **fit_params)[source]#

拟合数据,然后对其进行转换。

使用可选参数 fit_params 将转换器拟合到 Xy,并返回 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]#

获取转换的输出特征名称。

The feature names out will prefixed by the lowercased class name. For example, if the transformer outputs 3 features, then the feature names out are: ["class_name0", "class_name1", "class_name2"].

参数:
input_featuresarray-like of str or None, default=None

Only used to validate feature names with the names seen in fit.

返回:
feature_names_outstr 对象的 ndarray

转换后的特征名称。

get_metadata_routing()[source]#

获取此对象的元数据路由。

请查阅 用户指南,了解路由机制如何工作。

返回:
routingMetadataRequest

封装路由信息的 MetadataRequest

get_params(deep=True)[source]#

获取此估计器的参数。

参数:
deepbool, default=True

如果为 True,将返回此估计器以及包含的子对象(如果它们是估计器)的参数。

返回:
paramsdict

参数名称映射到其值。

inverse_transform(X)[source]#

将数据投影回其原始空间。

返回一个数组 X_original,该数组的变换将是 X。请注意,即使 X 是稀疏的,X_original 也是稠密的:这可能会使用大量内存。

如果 compute_inverse_components 为 False,则在每次调用 inverse_transform 时都会计算组件的反向,这可能会很昂贵。

参数:
X{array-like, sparse matrix} of shape (n_samples, n_components)

要被反向变换的数据。

返回:
X_original形状为 (n_samples, n_features) 的 ndarray

重构的数据。

set_output(*, transform=None)[source]#

设置输出容器。

有关如何使用 API 的示例,请参阅引入 set_output API

参数:
transform{“default”, “pandas”, “polars”}, default=None

配置 transformfit_transform 的输出。

  • "default": 转换器的默认输出格式

  • "pandas": DataFrame 输出

  • "polars": Polars 输出

  • None: 转换配置保持不变

1.4 版本新增: 添加了 "polars" 选项。

返回:
selfestimator instance

估计器实例。

set_params(**params)[source]#

设置此估计器的参数。

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

参数:
**paramsdict

估计器参数。

返回:
selfestimator instance

估计器实例。

transform(X)[source]#

通过与随机矩阵的点积来投影数据。

参数:
X{ndarray, sparse matrix} of shape (n_samples, n_features)

输入数据,将其投影到较低维度的空间。

返回:
X_newndarray of shape (n_samples, n_components)

投影后的数组。