clc
clear all
close all

data=importdata('IRIS.txt');
features=data(:,1:end-1);
[~,nof]=size(features);
lables=data(:,end);
% for i=1:1:length(lables)
%     if lables(i)>=5
%         lables(i)=lables(i)-1;
%     end
% end
[dim_red_data,WLDA,M,WPCA]=mylda(features,lables,2);
features1=dim_red_data;

group1=[];
group2=[];
group3=[];
% group4=[];
% group5=[];
% group6=[];
grp1=[];
grp2=[];
grp3=[];
% grp4=[];
% grp5=[];
% grp6=[];
% group7=[];
for i=1:1:length(lables)
    if lables(i)==1
        group1=[group1;features(i,:)];
        grp1=[grp1;features1(i,:)];
    elseif lables(i)==2
        group2=[group2;features(i,:)];
        grp2=[grp2;features1(i,:)];
    elseif lables(i)==3
        group3=[group3;features(i,:)];
        grp3=[grp3;features1(i,:)];
%     elseif lables(i)==4
%         group4=[group4;features(i,:)];
%         grp4=[grp4;features1(i,:)];
%     elseif lables(i)==5
%         group5=[group5;features(i,:)];
%         grp5=[grp5;features1(i,:)];
%     elseif lables(i)==6
%         group6=[group6;features(i,:)];
%         grp6=[grp6;features1(i,:)];
%     else
%         group7=[group7;features(i,:)];
    end
end
[n1,~]=size(group1);[n2,~]=size(group2);[n3,~]=size(group3);%[n4,~]=size(group4);
%[n5,~]=size(group5);[n6,~]=size(group6);%[n7,~]=size(group7);
m1=sum((group1-mean(group1)).^2);
m2=sum((group2-mean(group2)).^2);
m3=sum((group3-mean(group3)).^2);
% m4=sum((group4-mean(group4)).^2);
% m5=sum((group5-mean(group5)).^2);
% m6=sum((group6-mean(group6)).^2);
% m7=sum((group7-mean(group7)).^2);
ssw=m1+m2+m3;
sw2=ssw/(length(lables)-3);
feature_mean=mean(features);
mm1=n1*(mean(group1)-feature_mean).^2;
mm2=n2*(mean(group2)-feature_mean).^2;
mm3=n3*(mean(group3)-feature_mean).^2;
% mm4=n4*(mean(group4)-feature_mean).^2;
% mm5=n5*(mean(group5)-feature_mean).^2;
% mm6=n6*(mean(group6)-feature_mean).^2;
% mm7=n7*(mean(group7)-feature_mean).^2;
ssb=mm1+mm2+mm3;
sb2=ssb/2;
F_score=sb2./sw2;
subplot(211)
scatter(grp1(:,1),grp1(:,2),'*');
hold on
scatter(grp2(:,1),grp2(:,2),'*');
scatter(grp3(:,1),grp3(:,2),'*');

subplot(212)
scatter(group1(:,3),group1(:,4),'*');
hold on
scatter(group2(:,3),group2(:,4),'*');
scatter(group3(:,3),group3(:,4),'*');

figure
bar(F_score)