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%就行了。