pca主成份分析方法

1.应用pca的前提

  应用pca的前提是,连续信号具有相关性。相关性是什么,是冗余。就是要利用pca去除冗余。

2.pca的定义

  pca是一种去除随机变量间相关性的线性变换。是一种常用的多元数据分析方法。pca将互相关的输入数据转换成统计上不相干的主成分(或者特征),所得到的主成份通常是按照方差大小进行降序排列的。

reference :基于CCA的fMRI时空模型数据处理方法的研究,肖柯,硕士论文。

———————————————————下面来参考一下代码—————————————————————————————————---

参考:http://blog.csdn.net/watkinsong/article/details/8234766 

我在网上看了很多pca降维的例子,都大同小异,原理差不多,都是活的原来矩阵的协方差矩阵,然后计算协方差矩阵的特征值和特征向量,最后通过特征向量的根据特征值由大到小的排序进行KL变换神马的获得一个转换矩阵。
 

1. matlab自带的实现方式

 
 PCA在matlab中的实现举例
 
  以下资料来自matlab的help,翻译和注解部分由笔者添加:(重点部分添加了翻译!)
 
  princomp-----函数名称
 
  Principal component analysis (PCA) on data
 
  Syntax------函数调用语法
 
  [COEFF,SCORE] = princomp(X)
 
  [COEFF,SCORE,latent] = princomp(X)
 
  [COEFF,SCORE,latent,tsquare] = princomp(X)
 
  [...] = princomp(X,'econ')
 
  Description -----函数描述
 
  COEFF = princomp(X) performs principal components analysis (PCA) on the n-by-p data matrix X, and returns the principal component coefficients, also known as loadings. Rows of X correspond to observations, columns to variables. COEFF is a p-by-p matrix, each column containing coefficients for one principal component. The columns are in order of decreasing component variance.
 
  在n行p列的数据集X上做主成分分析。返回主成分系数。X的每行表示一个样本的观测值,每一列表示特征变量。COEFF是一个p行p列的矩阵,每一列包含一个主成分的系数,列是按主成分变量递减顺序排列。(按照这个翻译很难理解,其实COEFF是X矩阵所对应的协方差阵V的所有特征向量组成的矩阵,即变换矩阵或称投影矩阵,COEFF每列对应一个特征值的特征向量,列的排列顺序是按特征值的大小递减排序,后面有具体例子解释,见说明1)
 
  princomp centers X by subtracting off column means, but does not rescale the columns of X. To perform principal components analysis with standardized variables, that is, based on correlations, use princomp(zscore(X)). To perform principal components analysis directly on a covariance or correlation matrix, use pcacov.
 
  计算PCA的时候,MATLAB自动对列进行了去均值的操作,但是并不对数据进行规格化,如果要规格化的话,用princomp(zscore(X))。另外,如果直接有现成的协方差阵,用函数pcacov来计算。
 
  [COEFF,SCORE] = princomp(X) returns SCORE, the principal component scores; that is, the representation of X in the principal component space. Rows of SCORE correspond to observations, columns to components.
 
  返回的SCORE是对主分的打分,也就是说原X矩阵在主成分空间的表示。SCORE每行对应样本观测值,每列对应一个主成份(变量),它的行和列的数目和X的行列数目相同。
 
  [COEFF,SCORE,latent] = princomp(X) returns latent, a vector containing the eigenvalues of the covariance matrix of X.
 
  返回的latent是一个向量,它是X所对应的协方差矩阵的特征值向量。
 
  [COEFF,SCORE,latent,tsquare] = princomp(X) returns tsquare, which contains Hotelling's T2 statistic for each data point.
 
  返回的tsquare,是表示对每个样本点Hotelling的T方统计量(我也不很清楚是什么东东)。
 
  The scores are the data formed by transforming the original data into the space of the principal components. The values of the vector latent are the variance of the columns of SCORE. Hotelling's T2 is a measure of the multivariate distance of each observation from the center of the data set.
 
  所得的分(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')来计算,这样会显著提高计算速度
 
  Examples--举例
 
  (上面说了那么多废话,看了还不一定懂,还不如举例容易理解,下面样本数据集为ingredients,这个数据集是matlab自带的)
 
  Compute principal components for the ingredients data in the Hald data set, and the variance accounted for by each component.
 
  load hald; %载入matlab内部数据
 
  [pc,score,latent,tsquare] = princomp(ingredients); %调用pca分析函数
 
  ingredients,score,pc,latent,tsquare %显示得到的结果
 
  ingredients =
 
  7 26 6 60
 
  1 29 15 52
 
  11 56 8 20
 
  11 31 8 47
 
  7 52 6 33
 
  11 55 9 22
 
  3 71 17 6
 
  1 31 22 44
 
  2 54 18 22
 
  21 47 4 26
 
  1 40 23 34
 
  11 66 9 12
 
  10 68 8 12
 
  score =
 
  36.8218 -6.8709 -4.5909 0.3967
 
  29.6073 4.6109 -2.2476 -0.3958
 
  -12.9818 -4.2049 0.9022 -1.1261
 
  23.7147 -6.6341 1.8547 -0.3786
 
  -0.5532 -4.4617 -6.0874 0.1424
 
  -10.8125 -3.6466 0.9130 -0.1350
 
  -32.5882 8.9798 -1.6063 0.0818
 
  22.6064 10.7259 3.2365 0.3243
 
  -9.2626 8.9854 -0.0169 -0.5437
 
  -3.2840 -14.1573 7.0465 0.3405
 
  9.2200 12.3861 3.4283 0.4352
 
  -25.5849 -2.7817 -0.3867 0.4468
 
  -26.9032 -2.9310 -2.4455 0.4116
 
  pc =
 
  -0.0678 -0.6460 0.5673 0.5062
 
  -0.6785 -0.0200 -0.5440 0.4933
 
  0.0290 0.7553 0.4036 0.5156
 
  0.7309 -0.1085 -0.4684 0.4844
 
  latent =
 
  517.7969
 
  67.4964
 
  12.4054
 
  0.2372
 
  tsquare =
 
  5.6803
 
  3.0758
 
  6.0002
 
  2.6198
 
  3.3681
 
  0.5668
 
  3.4818
 
  3.9794
 
  2.6086
 
  7.4818
 
  4.1830
 
  2.2327
 
  2.7216
 
  %下面我们来做一个验证
 
  %下面为计算ingredients协方差矩阵:
 
  cov_ingredients=cov(ingredients)
 
  cov_ingredients =
 
  34.6026 20.9231 -31.0513 -24.1667
 
  20.9231 242.1410 -13.8782 -253.4167
 
  -31.0513 -13.8782 41.0256 3.1667
 
  -24.1667 -253.4167 3.1667 280.1667
 
  %下面为计算ingredients所对应的协方差矩阵(也就是cov_ingredients矩阵)的特征值和特征
 
  %向量,下面的矩阵V为特征向量,D为特征值(对比上面的latent)组成的对角线矩阵
 
  [V,D] = eig(cov_ingredients)
 
  V =
 
  0.5062 0.5673 0.6460 -0.0678
 
  0.4933 -0.5440 0.0200 -0.6785
 
  0.5156 0.4036 -0.7553 0.0290
 
  0.4844 -0.4684 0.1085 0.7309
 
  D =
 
  0.2372 0 0 0
 
  0 12.4054 0 0
 
  0 0 67.4964 0
 
  0 0 0 517.7969
 
  %说明1:对比一下矩阵V和矩阵pc,现在很容易明白为什么COEFF是按列递减顺序排列的
 
  % 了!(V中第三列与pc中倒数第三列差个负号,学过线性代数的人都知道这没问题)
 
  %下面再验证一下说明2
 
  diag(cov(score))
 
  ans =
 
  517.7969
 
  67.4964
 
  12.4054
 
  0.2372
 
  %说明2:以上结果显示latent确实表示SCORE矩阵每列的方差,517.7969表示第一列方差
 
  下面做图表示结果:
 
  上面说了半天还没有达到我们终极想要的,其实我们要的是由函数[pc,score,latent,tsquare] = princomp(ingredients)它所产生的pc和latent。由latent可以算出降维后的空间所能表示原空间的程度,只要这个累积的值大于95%就行了。
 
  The following command and plot show that two components account for 98% of the variance:
 
  cumsum(latent)./sum(latent)
 
  ans =
 
  0.86597
 
  0.97886
 
  0.9996
 
  1
 
  %由以上ans值可以看出前两个主成分就能表示原空间的97.886%,所以取pc中的前两列可
 
  %做主成分变换矩阵tranMatrix = pc(:,1:2)。则从原来的4维空间降到2维空间。对任意一个
 
  %原空间样本,例如a=(7 ,26 ,6 ,60)变到低维空间的表达式为a1 = a*tranMatrix。(当然你也可
 
  %以取pc中的前三列,由原来的4维空间变到3维空间)
 
  biplot(pc(:,1:2),'Scores',score(:,1:2),'VarLabels',...
 
  {'X1' 'X2' 'X3' 'X4'})

 

原文地址:https://www.cnblogs.com/haore147/p/3630002.html