主成分分析(PCA)——matlab程序及函数详解

转载自:https://blog.csdn.net/ckzhb/article/details/75281727
参考来源:


http://www.cnblogs.com/Hand-Head/articles/5156435.html

http://www.matlabsky.com/thread-11751-1-1.html


">程序源码下载链接http://download.csdn.net/detail/ckzhb/9903051

包含三个m文件:

drtool_pca:函数封装。不包括盒形图。
pca_test:matlab自带的例子——pca()函数
princomp_test:matlab自带的例子——princomp()函数


例子说明:

它使用了衡量美国329个城市生活质量的9个指标:气候、住房、健康、犯罪率、交通、教育、艺术、娱乐和经济。

对于各指标,越高表示越好,如高的犯罪指标表示低的犯罪率。

本文例子程序:

%% test for princomp(Principal Component Analysis)
% 20170706 BY Hubery_Zhang
clear
;
clc;
%% load data set
load cities;
%% box plot forratings data
% To get a quickimpression of the ratings data, make a box plot
figure;
boxplot(ratings,‘orientation’,‘horizontal’,‘labels’,categories);
grid on;
%% pre-process
stdr =std(ratings);
sr =ratings./repmat(stdr,329,1);
%% use princomp
[coeff,score,latent,tsquare]= princomp(sr);
%% 如何提取主成分,达到降为的目的
% 通过latent,可以知道提取前几个主成分就可以了.
% 图中的线表示的累积变量解释程度.
% 通过看图可以看出前七个主成分可以表示出原始数据的90%.
% 所以在90%的意义下只需提取前七个主成分即可,进而达到主成分提取的目的.
figure;
percent_explained= 100*latent/sum(latent); %cumsum(latent)./sum(latent)
pareto(percent_explained);
xlabel(‘PrincipalComponent’);
ylabel(‘VarianceExplained (%)’);
%% Visualizing theResults
% 横坐标和纵坐标分别表示第一主成分和第二主成分
% 红色的点代表329个观察量,其坐标就是那个score
% 蓝色的向量的方向和长度表示了每个原始变量对新的主成分的贡献,其坐标就是那个coeff.
figure;
biplot(coeff(:,1:2),‘scores’,score(:,1:2),…
‘varlabels’,categories);
axis([-.26 1 -.51.51]);

程序详解:

1、std()函数

(1)std(A)——函数求解的是最常见的标准差,此时除以的是N-1,按照列求标注差即输出每一列的标准差。

(2)std(A,flag)——flag代表的是用哪一种标准差函数,如果取0,则代表除以N-1,如果是1代表的是除以N。

(3)std(A,flag,dim)——dim代表的是按照列求标准差还是按照行求标准差,std(A,1,1)代表的是按照列求标准差,std(A,1,2)代表的是按照行求标准差。

2、B = repmat(A,m,n) 复制和平铺矩阵

将矩阵 A 复制 m×n 块,即把 A 作为 B 的元素,B 由 m×n 个 A 平铺而成。B 的维数是 [size(A,1)*m, size(A,2)n]


3、“./

矩阵中,点运算表示元素之间的运算。

小结:

%% pre-process

stdr = std(ratings);

sr = ratings./repmat(stdr,329,1);

使所有特征的方差相等即单位化,保证所有属性在同一个数量级上面。

note:

无论之前是否进行了去均值化(即每个数据减去特征的均值),通过此操作(求特征的标准差,然后每个数据除以标准差)均可以达到特征归一化。

当然,使用PCA必须进行归一化,函数princomp自动对列进行去均值化

princomp centers X bysubtracting off column means, but does not rescale the columns of X. To perform principal components analysis with standardizedvariables, that is, based on correlations, use princomp(zscore(X)). To perform principal components analysis directly on acovariance or correlation matrix, use pcacov.

4、[COEFF,SCORE,latent,tsquare] = princomp(X)

princomp函数未来被废弃,系统推荐使用pca函数。

X是n行P列的。n是数据样本个数,p是特征数。

(1)

COEFF is a p-by-p matrix, each columncontaining coefficients for one principal component. The columns are in orderof decreasing component variance.

主成分系数:即原始数据线性组合生成主成分数据中每一维数据前面的系数.COEFF的每一列代表一个新生成的主成分的系数.

(2)

 SCORE, the principal component scores; that is, the representationof X in the principal component space. Rows of SCORE correspond to observations, columns to components.n-by-p matrix

即原始数据在新生成的主成分空间里的坐标值.

(3)

 latent, a vector containing the eigenvalues of the covariancematrix of X.

即 latent = sort(eig(cov(sr)),‘descend’);

(4)

 tsquare, which contains Hotelling’s T2 statistic foreach data point.

一种多元统计距离,记录的是每一个观察量到中心的距离。

The scores are the data formed by transformingthe original data into the space of the principal components. The values of thevector latent are thevariance of the columns of SCORE. Hotelling’sT2 is a measure of the multivariate distance of each observationfrom the center of the data set.

When n <= pSCORE(:,n:p) and latent(n:p) arenecessarily zero, and the columns of COEFF(:,n:p) definedirections that are orthogonal to X.

5、percent_explained = 100latent/sum(latent);

或者%cumsum(latent)./sum(latent)

b=sum(a)函数:若a是向量,则求和即可;若a是矩阵,默认对列求和,可以改变dim参数进行按行求和。

b=cumsum(a)函数:当a是向量时,返回向量中各元素分别是该元素在a中位置之前所有元素之和如a=1 2 3,b=1 3 6。
当a是矩阵时,默认b中每一列都是对应列前面各行元素之和,当dim为2时,b的各行为对应行前面各列元素。
 
 

6、pareto(percent_explained);

Pareto charts display the values in thevector Y as bars drawn in descending order. Values in Y mustbe nonnegative and not include NaNs. Only the first 95% of the cumulativedistribution is displayed. also plots a line displaying the cumulative sum of Y.

7、biplot(coefs,‘Name’,Value)

biplot(coefs) creates a biplot of the coefficients in the matrix coefs. The biplot is 2-D if coefs has two columns or 3-D if it has three columns. coefs usually contains principal component coefficients createdwith pca, pcacov, or factor loadings estimated with factoran. The axes in the biplot represent the principal componentsor latent factors (columns of coefs), and the observed variables (rows of coefs) are represented as vectors.

比如在2-D图中,横坐标和纵坐标分别表示第一主成分和第二主成分;红色的点代表329个观察量,其坐标就是那个score;蓝色的向量的方向和长度表示了每个原始变量对新的主成分的贡献,其坐标就是那个coef.

‘Name’,Value)   

‘Scores’:

scores usually contains principal component scores created with pca or factor scores estimated with factoran. Each observation (row of scores) is represented as a pointin the biplot.

‘VarLabels’:

Labels each vector (variable) with the text inthe character array or cell array varlabels.






猜你喜欢

转载自blog.csdn.net/good_learner_1/article/details/88942013