PCA主成分分析法程序

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_39919527/article/details/82532824

%% test for princomp(Principal Component Analysis)  
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]);  

猜你喜欢

转载自blog.csdn.net/weixin_39919527/article/details/82532824