%% ʼռ
clc
clear
close all

%% 
load fisheriris

%% άʾ
% 곤Ⱥͻɢͼ
figure;
speciesNum = grp2idx(species);
gscatter(meas(:,3),meas(:,4),speciesNum,['r','g','b'])
% 곤ȺͿȵPCA
% measRe = score*coeff'+repmat(mu,size(score,1),1);
[coeff, score, latent, tsquared, explained, mu] = pca(meas(:,3:4));
hold on
plot(mu(1)+[0 coeff(1,1)],mu(2)+[0 coeff(2,1)],'r')
plot(mu(1)+[0 coeff(1,2)],mu(2)+[0 coeff(2,2)],'g')
hold off
axis equal
axis([-1 7 -1 7])
xlabel('곤')
ylabel('')

%% άʾ
%% ɷַ Principal Component Analysis
% measRe = score*coeff'+repmat(mu,size(score,1),1);
[coeff, score, latent, tsquared, explained, mu] = pca(meas);
% PLotMatrixɢͼ
% ʼͼ
hf  = figure;
hf.Units = 'Pixels';
% set(hf,'Units','Pixels');
hf.Position = [50 50 800 800];
% ɢͼ
speciesNum = grp2idx(species);
[H,AX,BigAx] = gplotmatrix(score,[],speciesNum,['r','g','b']);
legend(AX(13+3),{'Setosa ɽβ','Versicolor ɫβ','Virginica β'},'Location','northwest','FontWeight','Bold','Fontsize',10)
title(BigAx,'βPCAɢͼ','FontWeight','Bold','Fontsize',16)
%
xlabel(AX(4),{'Component 1';'ɷ1'},'FontWeight','Bold','Fontsize',12)
xlabel(AX(8+1),{'Component 2';'ɷ2'},'FontWeight','Bold','Fontsize',12)
xlabel(AX(12+2),{'Component 3','ɷ3'},'FontWeight','Bold','Fontsize',12)
xlabel(AX(16+3),{'Component 4','ɷ4'},'FontWeight','Bold','Fontsize',12)
% 
ylabel(AX(1),{'Component 1';'ɷ1'},'FontWeight','Bold','Fontsize',12)
ylabel(AX(2),{'Component 2';'ɷ2'},'FontWeight','Bold','Fontsize',12)
ylabel(AX(3),{'Component 3','ɷ3'},'FontWeight','Bold','Fontsize',12)
ylabel(AX(4),{'Component 4','ɷ4'},'FontWeight','Bold','Fontsize',12)
% Biplotͼ
hf2 = figure;
vbls={'Ƭ','Ƭ','곤',''};
biplot(coeff(:,1:3),'Score',score(:,1:3),'Varlabels',vbls);

