首页 > 技术文章 > matlab 主成分分析的函数 princomp()

Catherinezhilin 2021-07-30 00:39 原文

由于主成分分析(principile component analysis,PCA)这个概念在不同领域(统计学、数学等)的解释差异较大,所以,对 Matlab 中这个函数的理解与使用也稍有困难。本文通过使用对该函数做一点儿解释。

语法:

[COEFF,SCORE] = princomp(X)
[COEFF,SCORE,latent] = princomp(X)
[COEFF,SCORE,latent,tsquare] = princomp(X)
[...] = princomp(X,'econ')

 

1、输入参数 X 是一个 n 行 p 列的矩阵。每行代表一个样本观察数据,每列则代表一个属性,或特征。

 

2、COEFF 就是所需要的特征向量组成的矩阵,是一个 p 行 p 列的矩阵,没列表示一个出成分向量,经常也称为(协方差矩阵的)特征向量。并且是按照对应特征值降序排列的。所以,如果只需要前 k 个主成分向量,可通过:COEFF(:,1:k) 来获得。

 

3、SCORE 表示原数据在各主成分向量上的投影。但注意:是原数据经过中心化后在主成分向量上的投影(the representation of X in the principal component space.  Rows of SCORE correspond to observations, columns to components.)。即通过:SCORE = x0*COEFF 求得。其中 x0 是中心平移后的 X(注意:是对维度进行中心平移,而非样本。),因此在重建时,就需要加上这个平均值了。

 

4、latent 是一个列向量,表示特征值,并且按降序排列。(the principal component   variances, i.e., the eigenvalues of the covariance matrix of X)。

注意: 所得的分(scores)表示由原数据X转变到主成分空间所得到的数据。latent向量的值表示SCORE矩阵每列的方差(见说明2)。Hotelling的T方是用来衡量多变量间的距离,这个距离是指样本观测值到数据集中心的距离。

 

  When n <= p, SCORE(:,n:p) and latent(n:p) are necessarily zero, and the columns of COEFF(:,n:p) define directions that are orthogonal to X.

 

  [...] = princomp(X,'econ') returns only the elements of latent that are not necessarily zero, and the corresponding columns of COEFF and SCORE, that is, when n <= p, only the first n-1. This can be significantly faster when p is much larger than n.

 

  当维数p超过样本个数n的时候,用[...] = princomp(X,'econ')来计算,这样会显著提高计算速度

 
pca分量和累计解释方差图:
 

推荐阅读