randomized_svd#
- sklearn.utils.extmath.randomized_svd(M, n_components, *, n_oversamples=10, n_iter='auto', power_iteration_normalizer='auto', transpose='auto', flip_sign=True, random_state=None, svd_lapack_driver='gesdd')[源代码]#
计算截断随机 SVD。
此方法解决了 [1] (问题 (1.5), p5) 中描述的固定秩近似问题。
请参阅 Wikipedia principal eigenvector,了解用于对网页进行排名的幂迭代算法的典型示例。该算法也已知用作 Google PageRank 算法的构建块。
- 参数:
- M{array-like, sparse matrix} shape (n_samples, n_features)
要分解的矩阵。
- n_componentsint
要提取的奇异值和奇异向量的数量。
- n_oversamplesint, default=10
用于采样
M的范围的附加随机向量数量,以确保正确的条件。用于查找M范围的随机向量总数为n_components + n_oversamples。较少数量可以提高速度,但可能会对奇异向量和奇异值的近似质量产生负面影响。对于大型矩阵、噪声问题、谱衰减缓慢的矩阵或为提高精度精度,用户可能希望将此参数增加到2*k - n_components,其中 k 是有效秩。请参阅 [1] (第 5、23 和 26 页)。- n_iterint or ‘auto’, default=’auto’
幂迭代次数。可用于处理非常嘈杂的问题。当为 ‘auto’ 时,设置为 4,除非
n_components很小 (< .1 * min(X.shape)),在这种情况下,n_iter设置为 7。这可以提高具有少数组件的精度。请注意,通常用户应该增加n_oversamples而不是增加n_iter,因为随机方法的基本原理是避免使用这些更耗时的幂迭代步骤。当n_components等于或大于有效矩阵秩且谱不存在缓慢衰减时,理论上n_iter=0或1甚至应该正常工作 (请参阅 [1] 第 9 页)。版本 0.18 中已更改。
- power_iteration_normalizer{‘auto’, ‘QR’, ‘LU’, ‘none’}, default=’auto’
是否使用逐步 QR 分解(最慢但最准确)、‘none’(最快但当
n_iter较大时(通常为 5 或更大)数值不稳定)或 LU 分解(数值稳定但精度略有损失)来归一化幂迭代。‘auto’ 模式在n_iter<= 2 时不进行归一化,否则切换到 LU。版本 0.18 新增。
- transposebool or ‘auto’, default=’auto’
算法是否应用于 M.T 而不是 M。结果应大致相同。当 M.shape[1] > M.shape[0] 时,“auto”模式将触发转置,因为此随机 SVD 实现在此情况下速度更快。
版本 0.18 中已更改。
- flip_signbool, default=True
奇异值分解的输出仅在奇异向量符号的排列 up to 唯一。如果
flip_sign设置为True,则通过使左奇异向量中每个分量的最大负载为正来解决符号歧义。- random_stateint, RandomState instance or None, default=’warn’
在混洗数据时使用的伪随机数生成器的种子,即获取初始化算法的随机向量。传递一个整数以获得多次函数调用的可重复结果。请参阅 Glossary。
版本 1.2 中已更改: 默认值从 0 更改为 None。
- svd_lapack_driver{“gesdd”, “gesvd”}, default=”gesdd”
是使用更高效的“分治法”(
"gesdd")还是更通用的“矩形法”("gesvd")来计算矩阵 B 的 SVD,B 是 M 到低维子空间的投影,如 [1] 中所述。1.2 版本新增。
- 返回:
- undarray of shape (n_samples, n_components)
具有符号翻转的左奇异向量作为列的酉矩阵。
- sndarray of shape (n_components,)
奇异值,按降序排序。
- vhndarray of shape (n_components, n_features)
具有符号翻转的右奇异向量作为行的酉矩阵。
注意事项
该算法使用随机化来加速计算,从而找到(通常非常好)的近似截断奇异值分解。对于您希望仅提取少量组件的大型矩阵,它特别快。为了进一步加快速度,可以将
n_iter设置为 <=2(以损失精度为代价)。为了提高精度,建议增加n_oversamples,直到2*k-n_components,其中 k 是有效秩。通常,n_components选择大于 k,因此将n_oversamples增加到n_components应该足够了。References
[1] (1,2,3,4)“Finding structure with randomness: Stochastic algorithms for constructing approximate matrix decompositions” Halko, et al. (2009)
[2]“A randomized algorithm for the decomposition of matrices” Per-Gunnar Martinsson, Vladimir Rokhlin and Mark Tygert (2011)
[3]“An implementation of a randomized algorithm for principal component analysis” A. Szlam et al. (2014)
示例
>>> import numpy as np >>> from sklearn.utils.extmath import randomized_svd >>> a = np.array([[1, 2, 3, 5], ... [3, 4, 5, 6], ... [7, 8, 9, 10]]) >>> U, s, Vh = randomized_svd(a, n_components=2, random_state=0) >>> U.shape, s.shape, Vh.shape ((3, 2), (2,), (2, 4))