转载请声明出处。by watkins song
PCA的一些基本资料
最近因为最人脸表情识别,提取的gabor特征太多了,所以需要用PCA进行对提取的特征进行降维。
本来最早的时候我没有打算对提取的gabor特征进行降维,但是如果一个图像时64*64,那么使用五个尺度八个方向的gabor滤波器进行滤波,这样提取的特征足足有64*64*5*8这么多,如果图像稍微大一点,比如128*128的图像,那么直接提取的特征就会几十万,所以不降维的话直接用SVM训练分类器是非常困难的。
所以在这段时间我就学习了一下PCA降维的基本原理和使用方法,网上给出的资料都比较乱,而且很不清楚,经过这几天的学习和测试,终于把调理弄清楚了,给大家分享一下,下面只是我对于PCA的个人理解,肯定有不对的地方,还请各位大牛多多指教。
下面先给出一下PCA的资料地址,都是我收集的:
http://hi.baidu.com/yicomrdztxbeiwd/item/913f28c05cf7ebc4994aa06f
http://blog.sciencenet.cn/blog-265205-544681.html
http://blog.csdn.net/mpbchina/article/details/7384425
http://blog.sina.com.cn/s/blog_6833a4df0100pvk7.html
http://stackoverflow.com/questions/4991343/matlab-principal-component-analysis-eigenvalues-order
http://stackoverflow.com/questions/10400230/what-is-score-in-princomp
http://www.mathworks.com/matlabcentral/newsreader/view_thread/152608
http://stats.stackexchange.com/questions/27572/matlab-princomp-latent
http://www.nlpca.org/pca-principal-component-analysis-matlab.html
http://www.matlabsky.com/thread-11751-1-1.html
http://stackoverflow.com/questions/10818718/principal-component-analysis
http://www.mathworks.cn/cn/help/stats/princomp.html
http://www.mathworks.cn/cn/help/stats/pca.html#bti6n7k-2
http://lovelittlebean.blog.163.com/blog/static/116582186201181213911729/
http://www.ilovematlab.cn/thread-54493-1-1.html
http://www.ilovematlab.cn/forum.php?mod=viewthread&tid=146626
http://www.ilovematlab.cn/forum.php?mod=viewthread&tid=204069
http://www.ilovematlab.cn/forum.php?mod=viewthread&tid=54600
http://search.discuz.qq.com/s/aa8585553/princomp+%E9%99%8D%E7%BB%B4.html
http://www.ilovematlab.cn/thread-68796-1-1.html
http://www.ilovematlab.cn/thread-209229-1-1.html
http://www.ilovematlab.cn/thread-209229-1-1.html
http://blog.sina.com.cn/s/blog_61c0518f0100f4mi.html
http://blog.csdn.net/haitao111313/article/details/7875392
http://media.cs.tsinghua.edu.cn/~ahz/digitalimageprocess/chapter11/chapt11_ahz.htm
http://hi.baidu.com/845777018/item/7438e555df1138404fff2011
http://en.wikipedia.org/wiki/Principal_component_analysis
http://baike.baidu.com/view/852194.htm
http://wenku.baidu.com/view/bd9284fcfab069dc51220107.html
http://wenku.baidu.com/view/c0bde56da98271fe910ef9b8.html
http://wenku.baidu.com/view/9f69930790c69ec3d5bb75d3.html
http://www.ilovematlab.cn/thread-54600-1-1.html
http://www.cnblogs.com/sunwufan/archive/2011/08/31/2159952.html
http://zhidao.baidu.com/question/416895922.html
上面的网址都是一些pca原理啊,实现什么的介绍。
具体的PCA的算法的理论基础呢,我这里就不详细说了,因为我也没有看具体详细,所以如果想要彻底的弄明白PCA的工作原来,还是请到wiki上看吧,写的非常清晰,我因为临时用一下,就写个大致的原理就可以了。
PCA原理:
PCA的原理就是将原来的样本数据投影到一个新的空间中,相当于我们在矩阵分析里面学习的将一组矩阵映射到另外的坐标系下。通过一个转换坐标,也可以理解成把一组坐标转换到另外一组坐标系下,但是在新的坐标系下,表示原来的原本不需要那么多的变量,只需要原来样本的最大的一个线性无关组的特征值对应的空间的坐标即可。
比如,原来的样本是30*1000000的维数,就是说我们有30个样本,每个样本有1000000个特征点,这个特征点太多了,我们需要对这些样本的特征点进行降维。那么在降维的时候会计算一个原来样本矩阵的协方差矩阵,这里就是1000000*1000000,当然,这个矩阵太大了,计算的时候有其他的方式进行处理,这里只是讲解基本的原理,然后通过这个1000000*1000000的协方差矩阵计算它的特征值和特征向量,最后获得具有最大特征值的特征向量构成转换矩阵。比如我们的前29个特征值已经能够占到所有特征值的99%以上,那么我们只需要提取前29个特征值对应的特征向量即可。这样就构成了一个1000000*29的转换矩阵,然后用原来的样本乘以这个转换矩阵,就可以得到原来的样本数据在新的特征空间的对应的坐标。30*1000000
* 1000000*29 = 30 *29, 这样原来的训练样本每个样本的特征值的个数就降到了29个。
一般来说,PCA降维后的每个样本的特征的维数,不会超过训练样本的个数,因为超出的特征是没有意义的。
下面是百度百科中对pca降维的一段解释,还是挺清晰的:
“对于一个训练集,100个对象模板,特征是10维,那么它可以建立一个100*10的矩阵,作为样本。求这个样本的协方差矩阵,得到一个10*10的协方差矩阵,然后求出这个协方差矩阵的特征值和特征向量,应该有10个特征值和特征向量,我们根据特征值的大小,取前四个特征值所对应的特征向量,构成一个10*4的矩阵,这个矩阵就是我们要求的特征矩阵,100*10的样本矩阵乘以这个10*4的特征矩阵,就得到了一个100*4的新的降维之后的样本矩阵,每个特征的维数下降了。
当给定一个测试的特征集之后,比如1*10维的特征,乘以上面得到的10*4的特征矩阵,便可以得到一个1*4的特征,用这个特征去分类。”
我对 PCA的一些了解
我的pca迷惑
迷惑一
迷惑三
pca的实现(matlab)
1. 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'})
not necessarily zero, i.e., when N <= P, only the first N-1, and the
corresponding columns of COEFF and SCORE. This can be significantly
faster when P >> N.
cumsum(latent)./sum(latent)
,通过这样计算特征值的累计贡献率,一般来说都选择前95%的特征值对应的特征向量,还是原来的矩阵30*1000000,如果你计算得到前25个特征值的累计贡献率已经超过99.9%,那么就完全可以只要降维后的数据的前25列。
2. 一个自实现的pca降维方式
- %训练
- %Lx=X'*X
- clear;
- clc;
- train_path='..\Data\TrainingSet\';
- phi=zeros(64*64,20);
- for i=1:20
- path=strcat(train_path,num2str(i),'.bmp');
- Image=imread(path);
- Image=imresize(Image,[64,64]);
- phi(:,i)=double(reshape(Image,1,[])');
- end;
- %mean
- mean_phi=mean(phi,2);
- mean_face=reshape(mean_phi,64,64);
- Image_mean=mat2gray(mean_face);
- imwrite(Image_mean,'meanface.bmp','bmp');
- %demean
- for i=1:19
- X(:,i)=phi(:,i)-mean_phi;
- end
- Lx=X'*X;
- tic;
- [eigenvector,eigenvalue]=eigs(Lx,19);
- toc;
- %normalization
- for i=1:19
- %K-L变换