如何select第一个组件并计算PCA中的变异百分比?
How to select first component and calculate percentage of variation in PCA?
我有一个矩阵 M,其中列是数据点,行是特征。现在我想做 PCA 和 select 只有第一个具有最高方差的组件。
我知道我可以在 Matlab 中使用 [coeff,score,latent] = pca(M') 来完成。首先我想我必须转置矩阵 M.
我怎样才能select现在第一个组件?我不确定三种不同的输出矩阵。
其次,我还想计算每个分量解释的方差百分比。我该怎么做?
确实,您应该将输入转置为将行作为数据点,将列作为特征:
[coeff, score, latent, ~, explained] = pca(M');
主成分由 coeff
的列按方差降序排列,因此第一列包含最重要的成分。每个分量的方差在 latent
中给出,解释的总方差百分比在 explained
.
中给出
firstCompCoeff = coeff(:,1);
firstCompVar = latent(1);
更多信息:pca
documentation.
请注意,pca
函数需要统计工具箱。如果没有,您可以在互联网上搜索替代方案或使用 svd
.
自行实施
如果矩阵的维度为 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
我有一个矩阵 M,其中列是数据点,行是特征。现在我想做 PCA 和 select 只有第一个具有最高方差的组件。
我知道我可以在 Matlab 中使用 [coeff,score,latent] = pca(M') 来完成。首先我想我必须转置矩阵 M.
我怎样才能select现在第一个组件?我不确定三种不同的输出矩阵。
其次,我还想计算每个分量解释的方差百分比。我该怎么做?
确实,您应该将输入转置为将行作为数据点,将列作为特征:
[coeff, score, latent, ~, explained] = pca(M');
主成分由 coeff
的列按方差降序排列,因此第一列包含最重要的成分。每个分量的方差在 latent
中给出,解释的总方差百分比在 explained
.
firstCompCoeff = coeff(:,1);
firstCompVar = latent(1);
更多信息:pca
documentation.
请注意,pca
函数需要统计工具箱。如果没有,您可以在互联网上搜索替代方案或使用 svd
.
如果矩阵的维度为 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