簡單的主成分分析。第一次見識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)
還有,運行到最后會播放一段振奮人心的歌曲哈!
