MATLAB做主成分分析(PCA)

Posted on 2016-09-17 00:32  不秩稚童  阅读(7508)  评论(0编辑  收藏  举报

  简单的主成分分析。第一次见识PCA,我的认识是,尽量用更少的维度来描述数据,以达到理想(虽不是最好,但是''性价比''最高)的效果。

%% 主成分分析降维
clear;
% 参数初始化
inputfile = 'F:\Techonolgoy\MATLAB\file\MTALAB数据分析与挖掘实战\Datasets\chapter4\chapter4\示例程序\data\principal_component.xls';
outputfile = 'F:\Techonolgoy\MATLAB\file\MTALAB数据分析与挖掘实战\4\dimention_reducted.xls';
proporition = 0.95;
%% 数据读取
[num,~] = xlsread(inputfile);
%% 主成分分析
[coeff,~,latent] = pca(num);    %coeff每列为特征向量,latent为对应特征值
%% 计算累计贡献度,确认维度
sum_latent = cumsum(latent/sum(latent));  % 累计贡献率
dimension = find(sum_latent>proporition);
dimension = dimension(1);

%% 降维
data = num*coeff(:,1:dimension);
xlswrite(outputfile,data);

disp('主成分特征根:');
disp(latent');
disp('主成分单位特征向量:');
disp('累计贡献率');
disp(sum_latent');
disp(['主成分分析完成,降维后的数据在'  outputfile])

%哈里路亚
load handel
sound(y,Fs)

  还有,运行到最后会播放一段振奋人心的歌曲哈!