【问题标题】:sklearn PCA not workingsklearn PCA 不工作
【发布时间】:2015-09-27 00:33:07
【问题描述】:

我一直在玩 sklearn PCA,它的行为很奇怪。

from sklearn.decomposition import PCA
import numpy as np
identity = np.identity(10)
pca = PCA(n_components=10)
augmented_identity = pca.fit_transform(identity)
np.linalg.norm(identity - augmented_identity)

4.5997749080745738

请注意,我将维数设置为 10。范数不应该是 0 吗?

任何关于为什么不这样做的见解将不胜感激。

【问题讨论】:

    标签: python numpy scikit-learn pca


    【解决方案1】:

    虽然 PCA 基于协方差矩阵计算正交分量,但 sklearn 中 PCA 的输入是数据矩阵,而不是协方差/相关矩阵。

    import numpy as np
    from sklearn.decomposition import PCA
    
    # gaussian random variable, 10-dimension, identity cov mat
    X = np.random.randn(100000, 10)
    
    
    
    pca = PCA(n_components=10)
    X_transformed = pca.fit_transform(X)
    
    np.linalg.norm(np.cov(X.T) - np.cov(X_transformed.T))
    
    Out[219]: 0.044691263454134933
    

    【讨论】:

      猜你喜欢
      • 2017-09-21
      • 2021-03-24
      • 2020-04-02
      • 2017-07-19
      • 1970-01-01
      • 2019-04-03
      • 2020-12-22
      • 1970-01-01
      • 2018-09-13
      相关资源
      最近更新 更多