1. 程式人生 > >Iris資料集用主成分分析MATLAB

Iris資料集用主成分分析MATLAB

1、程式碼

filename = 'iris.csv';
data = csvread(filename);
rawdata = data(:,2:5)
a = data(data(:,1)==0,:);
a = a(:,2:5);
b = data(data(:,1)==1,:);
b = b(:,2:5);
c = data(data(:,1)==2,:);
c = c(:,2:5);
[coefs,scores,variances,t2] = princomp(rawdata);
a=a*coefs(:,1:2);
b=b*coefs(:,1:2);
c=c*coefs(:,1:2);
plot
(a(:,1),a(:,2),'r.','markersize',20); hold on plot(b(:,1),b(:,2),'g.','markersize',20); hold on plot(c(:,1),c(:,2),'b.','markersize',20); hold off xlabel('1st Principal Component'); ylabel('2nd Principal Component');

2、結果

在這裡插入圖片描述