【问题标题】:Performing svd by sklearn.decomposition.PCA , how can I get the U S V from this?通过 sklearn.decomposition.PCA 执行 svd ,我怎样才能从中获得 US V?
【发布时间】:2017-04-04 15:22:49
【问题描述】:

我用 sklearn.decomposition.PCA 执行 SVD

从 SVD 的方程

A= U x S x V_t

V_t = V 的转置矩阵 (对不起,我无法粘贴原方程)

如果我想要矩阵 U、S 和 V,如果我使用 sklearn.decomposition.PCA 怎么得到它?

【问题讨论】:

    标签: python pca svd


    【解决方案1】:

    首先,根据矩阵的大小,PCA 的 sklearn 实现并不总是计算完整的 SVD 分解。以下摘自PCA's GitHub reciprocity

    svd_solver : string {'auto', 'full', 'arpack', 'randomized'}
            auto :
                the solver is selected by a default policy based on `X.shape` and
                `n_components`: if the input data is larger than 500x500 and the
                number of components to extract is lower than 80% of the smallest
                dimension of the data, then the more efficient 'randomized'
                method is enabled. Otherwise the exact full SVD is computed and
                optionally truncated afterwards.
            full :
                run exact full SVD calling the standard LAPACK solver via
                `scipy.linalg.svd` and select the components by postprocessing
            arpack :
                run SVD truncated to n_components calling ARPACK solver via
                `scipy.sparse.linalg.svds`. It requires strictly
                0 < n_components < X.shape[1]
            randomized :
                run randomized SVD by the method of Halko et al.
    

    此外,它还对数据进行一些操作(参见here)。

    现在,如果您想获得在sklearn.decomposition.PCA 中使用的U, S, V,您可以使用pca._fit(X)。 例如:

    from sklearn.decomposition import PCA
    X = np.array([[1, 2], [3,5], [8,10], [-1, 1], [5,6]])
    pca = PCA(n_components=2)
    pca._fit(X)
    

    打印

    (array([[ -3.55731195e-01,   5.05615563e-01],
            [  2.88830295e-04,  -3.68261259e-01],
            [  7.10884729e-01,  -2.74708608e-01],
            [ -5.68187889e-01,  -4.43103380e-01],
            [  2.12745524e-01,   5.80457684e-01]]),
     array([ 9.950385  ,  0.76800941]),
     array([[ 0.69988535,  0.71425521],
            [ 0.71425521, -0.69988535]]))
    

    但是,如果你只是想对原始数据进行 SVD 分解,我建议使用scipy.linalg.svd

    【讨论】:

    • 我不熟悉python,所以对我来说很难。还有一个问题是,如果数据量非常大,“随机”和“完整”之间哪个更好。我应该为我的数据集选择什么? “随机”和“完整”的结果是否不同?
    • 一个选项就是不选择,然后默认为'auto',求解器将选择使用哪种方法(基于矩阵大小和组件数)。这个想法是,如果矩阵非常大并且您不需要所有组件(也就是说,您需要数据最小维度的 80%),那么执行随机 svd 会更有效,并且您不会丢失太多这样做。在任何情况下,如果您确实需要所有组件(或超过 80%),那么 'auto' 保证将使用完整的 svd。随机 svd 基于这篇论文:arxiv.org/pdf/0909.4061.pdf
    猜你喜欢
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 2019-12-12
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 2017-05-25
    相关资源
    最近更新 更多