【问题标题】:How to select first component and calculate percentage of variation in PCA?如何选择第一个组件并计算 PCA 中的变异百分比?
【发布时间】:2015-08-19 14:11:49
【问题描述】:

我有一个矩阵 M,其中列是数据点,行是特征。现在我想做 PCA,只选择方差最大的第一个组件。

我知道我可以在 Matlab 中用 [coeff,score,latent] = pca(M') 做到这一点。首先我想我必须转置矩阵M。

我现在如何选择第一个组件?我不确定这三个不同的输出矩阵。

其次,我还想计算每个组件解释的方差百分比。我该怎么做?

【问题讨论】:

    标签: matlab pca


    【解决方案1】:

    如果您的矩阵的维度为 m x n,其中 m 是个案,n 是变量:

    % First you might want to normalize the matrix...
    M = normalize(M);
    
    % means very close to zero
    round(mean(M),10)
    
    % standard deviations all one
    round(std(M),10)
    
    % Perform a singular value decomposition of the matrix 
    [U,S,V] = svd(M);
    
    % First Principal Component is the first column of V
    V(:,1)
    
    % Calculate percentage of variation
    (var(S) / sum(var(S))) * 100
    

    【讨论】:

      【解决方案2】:

      确实,您应该转置输入以将行作为数据点,将列作为特征:

      [coeff, score, latent, ~, explained] = pca(M');
      

      主成分由coeff的列按方差降序给出,因此第一列包含最重要的成分。每个组件的方差在latent 中给出,解释的总方差百分比在explained 中给出。

      firstCompCoeff = coeff(:,1);
      firstCompVar = latent(1);
      

      欲了解更多信息:pca documentation。

      请注意,pca 函数需要统计工具箱。如果您没有,您可以在互联网上搜索替代方案或使用svd 自行实施。

      【讨论】:

      • 感谢您的回答。假设我们有 M=rand(100,30),即我们有 30 个数据点和 100 个特征。如果我现在进行 PCA,则 coeff 的尺寸为 100 x 29。如果我采用第一个组件,我仍然有 100 个特征。如何减少特征?
      • 我假设您再次转置,以便获得 30 行(数据点)和 100 列(特征)。 coeff 应该是 100x100(特征数量的方阵)。
      • 您新的简化特征是原始特征与计算系数的线性组合。因此,例如,如果您只想保留最主要的功能,您可以从上面获取firstCompCoeff。假设您现在有 10 个新数据点 Mnew = rand(10,100);,您现在可以使用 Mnew_reduced = Mnew * firstCompCoeff; 从它们那里获得缩减的特征。
      猜你喜欢
      • 1970-01-01
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 2016-07-12
      • 2022-01-16
      • 1970-01-01
      相关资源
      最近更新 更多