简体   繁体   English

scikit-learn TruncatedSVD解释的方差比不是降序

[英]scikit-learn TruncatedSVD's explained variance ratio not in descending order

The TruncatedSVD's explained variance ratio is not in descending order, unlike sklearn's PCA. 与sklearn的PCA不同,TruncatedSVD的解释方差比率不是降序。 I looked at the source code and it seems they use different way of calculating the explained variance ratio: 我查看了源代码,似乎他们使用不同的方式计算解释的方差比:

TruncatedSVD : TruncatedSVD

U, Sigma, VT = randomized_svd(X, self.n_components,
                              n_iter=self.n_iter,
                              random_state=random_state)
X_transformed = np.dot(U, np.diag(Sigma))
self.explained_variance_ = exp_var = np.var(X_transformed, axis=0)
if sp.issparse(X):
    _, full_var = mean_variance_axis(X, axis=0)
    full_var = full_var.sum()
else:
    full_var = np.var(X, axis=0).sum()
self.explained_variance_ratio_ = exp_var / full_var

PCA : PCA

U, S, V = linalg.svd(X, full_matrices=False)
explained_variance_ = (S ** 2) / n_samples
explained_variance_ratio_ = (explained_variance_ /
                             explained_variance_.sum())

PCA uses sigma to directly calculate the explained_variance and since sigma is in descending order, the explained_variance is also in the descending order. PCA使用sigma直接计算explain_variance,由于sigma按降序排列,explain_variance也按降序排列。 On the other hand, TruncatedSVD uses the variance of the columns of transformed matrix to calculate the explained_variance and therefore the variances are not necessarily in descending order. 另一方面, TruncatedSVD使用变换矩阵的列的方差来计算explain_variance,因此方差不一定按降序排列。

Does this mean that I need to sort the explained_variance_ratio from TruncatedSVD first in order to find the top k principle components? 这是否意味着我需要的那种explained_variance_ratioTruncatedSVD一是为了找到前k个主成分?

You dont have to sort explianed_variance_ratio , output itself would be sorted and contains only the n_component number of values. 您不必对explianed_variance_ratio进行排序,输出本身将被排序并仅包含n_component值的值。
From Documentation : 来自文档

TruncatedSVD implements a variant of singular value decomposition (SVD) that only computes the largest singular values, where k is a user-specified parameter. TruncatedSVD实现奇异值分解(SVD)的变体,其仅计算最大的奇异值,其中k是用户指定的参数。

X_transformed contains the decomposition using only k components. X_transformed包含仅使用k个分量的分解。

The example would give you an idea 这个例子会给你一个想法

>>> from sklearn.decomposition import TruncatedSVD
>>> from sklearn.random_projection import sparse_random_matrix
>>> X = sparse_random_matrix(100, 100, density=0.01, random_state=42)
>>> svd = TruncatedSVD(n_components=5, n_iter=7, random_state=42)
>>> svd.fit(X)  
TruncatedSVD(algorithm='randomized', n_components=5, n_iter=7,
        random_state=42, tol=0.0)
>>> print(svd.explained_variance_ratio_)  
[0.0606... 0.0584... 0.0497... 0.0434... 0.0372...]
>>> print(svd.explained_variance_ratio_.sum())  
0.249...
>>> print(svd.singular_values_)  
[2.5841... 2.5245... 2.3201... 2.1753... 2.0443...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM