TruncatedSVD#

class sklearn.decomposition.TruncatedSVD(n_components=2, *, algorithm='randomized', n_iter=5, n_oversamples=10, power_iteration_normalizer='auto', random_state=None, tol=0.0)[source]#

使用截断 SVD(又名 LSA)进行降维。

此转换器通过截断奇异值分解(SVD)执行线性降维。与PCA不同,此估计器在计算奇异值分解之前不会对数据进行中心化。这意味着它可以高效地处理稀疏矩阵。

特别是,截断SVD适用于 sklearn.feature_extraction.text 中矢量化器返回的术语计数/tf-idf矩阵。在这种情况下,它被称为潜在语义分析(LSA)。

此估计器支持两种算法:一种快速的随机SVD求解器,以及一种“朴素”算法,该算法使用ARPACK作为 X * X.TX.T * X 的特征求解器,以更高效者为准。

用户指南中了解更多信息。

参数:
n_componentsint, default=2

输出数据的所需维度。如果 algorithm='arpack',则必须严格小于特征数量。如果 algorithm='randomized',则必须小于或等于特征数量。默认值对于可视化非常有用。对于LSA,建议值为100。

algorithm{‘arpack’, ‘randomized’}, default=’randomized’

要使用的SVD求解器。要么是用于 SciPy 中 ARPACK 包装器(scipy.sparse.linalg.svds)的“arpack”,要么是用于 Halko (2009) 提出的随机算法的“randomized”。

n_iterint, default=5

随机 SVD 求解器的迭代次数。ARPACK 不使用此参数。默认值大于 randomized_svd 中的默认值,以处理可能具有缓慢衰减的大谱的稀疏矩阵。

n_oversamplesint, default=10

随机 SVD 求解器的过采样次数。ARPACK 不使用此参数。有关完整说明,请参阅 randomized_svd

版本 1.1 中新增。

power_iteration_normalizer{‘auto’, ‘QR’, ‘LU’, ‘none’}, default=’auto’

随机 SVD 求解器的幂迭代归一化器。ARPACK 不使用此参数。有关更多详细信息,请参阅 randomized_svd

版本 1.1 中新增。

random_stateint, RandomState instance or None, default=None

用于随机 SVD 期间。传入一个整数可确保跨多个函数调用获得可重现的结果。请参阅 词汇表

tolfloat, default=0.0

ARPACK 的容差。0 表示机器精度。随机 SVD 求解器忽略此参数。

属性:
components_ndarray of shape (n_components, n_features)

输入数据的右奇异向量。

explained_variance_shape 为 (n_components,) 的 ndarray

通过投影到每个组件转换后的训练样本的方差。

explained_variance_ratio_shape 为 (n_components,) 的 ndarray

每个选定组件解释的方差百分比。

singular_values_shape 为 (n_components,) 的 ndarray

与每个选定分量对应的奇异值。奇异值等于较低维空间中 n_components 个变量的 2-范数。

n_features_in_int

拟合 期间看到的特征数。

0.24 版本新增。

feature_names_in_shape 为 (n_features_in_,) 的 ndarray

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

1.0 版本新增。

另请参阅

DictionaryLearning

查找一个稀疏编码数据的字典。

FactorAnalysis

具有高斯潜在变量的简单线性生成模型。

IncrementalPCA

增量主成分分析。

KernelPCA

核主成分分析。

NMF

非负矩阵分解。

PCA

主成分分析。

注意事项

SVD 存在一个称为“符号不确定性”的问题,这意味着 components_ 的符号和转换的输出取决于算法和随机状态。为了解决这个问题,将此类实例拟合到数据一次,然后保留该实例以进行转换。

References

Halko, et al. (2009). “Finding structure with randomness: Stochastic algorithms for constructing approximate matrix decompositions”(通过随机性寻找结构:用于构建近似矩阵分解的随机算法)

示例

>>> from sklearn.decomposition import TruncatedSVD
>>> from scipy.sparse import csr_matrix
>>> import numpy as np
>>> np.random.seed(0)
>>> X_dense = np.random.rand(100, 100)
>>> X_dense[:, 2 * np.arange(50)] = 0
>>> X = csr_matrix(X_dense)
>>> svd = TruncatedSVD(n_components=5, n_iter=7, random_state=42)
>>> svd.fit(X)
TruncatedSVD(n_components=5, n_iter=7, random_state=42)
>>> print(svd.explained_variance_ratio_)
[0.0157 0.0512 0.0499 0.0479 0.0453]
>>> print(svd.explained_variance_ratio_.sum())
0.2102
>>> print(svd.singular_values_)
[35.2410  4.5981   4.5420  4.4486  4.3288]
fit(X, y=None)[source]#

在训练数据 X 上拟合模型。

参数:
Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}

训练数据。

y被忽略

Not used, present here for API consistency by convention.

返回:
selfobject

返回转换器对象。

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

将模型拟合到 X 并对 X 执行降维。

参数:
Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}

训练数据。

y被忽略

Not used, present here for API consistency by convention.

返回:
X_newndarray of shape (n_samples, n_components)

X 的降维版本。这将始终是一个密集数组。

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 转换回其原始空间。

返回数组 X_original,其转换将是 X。

参数:
Xarray-like of shape (n_samples, n_components)

New data.

返回:
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 执行降维。

参数:
Xshape 为 (n_samples, n_features) 的 {array-like, sparse matrix}

New data.

返回:
X_newndarray of shape (n_samples, n_components)

X 的降维版本。这将始终是一个密集数组。