clear all;
close all;
load fisheriris;
data = [meas(:,1), meas(:,2)]; %features selection
groups = ismember(species,'setosa');
[train, test] = crossvalind('holdOut',groups);
cp = classperf(groups);
svmStruct = svmtrain(data(train,:),groups(train),'showplot',true,'Kernel_Function','linear','boxconstraint',1000);
pause;
classes = svmclassify(svmStruct,data(test,:),'showplot',true);
classperf(cp,classes,test);
ccp = cp.CorrectRate % the accuracy 
pause;
hold on; %show the expected classification results with expect to the svm results
gr1=data(groups==1,:);
gr=data(groups==0,:);
plot(gr1(:,1),gr1(:,2),'b.');
plot(gr(:,1),gr(:,2),'r.');
hold off
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 groups 1 against all
close all;
gr1 = ismember(species,'setosa');
gr2=ismember(species,'versicolor');
gr3=ismember(species,'virginica');
[tr1, te1] = crossvalind('holdOut',gr1);
cp1 = classperf(gr1);
[tr2, te2] = crossvalind('holdOut',gr2);
cp2 = classperf(gr2);
[tr3, te3] = crossvalind('holdOut',gr3);
cp3 = classperf(gr3);
svmStr1 = svmtrain(data(tr1,:),gr1(tr1),'showplot',true,'Kernel_Function','rbf','RBF_Sigma',0.5,'boxconstraint',1000);
cl1 = svmclassify(svmStr1,data(tr1,:),'showplot',false);
classperf(cp1,cl1,tr1);
cp1.CorrectRate
pause;
cl1 = svmclassify(svmStr1,data(te1,:),'showplot',true);
classperf(cp1,cl1,te1);
cr1=cp1.CorrectRate
cl1 = svmclassify(svmStr1,data,'showplot',false);
pause;

% hold on;
figure;
svmStr2 = svmtrain(data(tr2,:),gr2(tr2),'showplot',true,'Kernel_Function','rbf','RBF_Sigma',0.5,'boxconstraint',1000);
cl2 = svmclassify(svmStr2,data(tr2,:),'showplot',false);
classperf(cp2,cl2,tr2);
cp2.CorrectRate
pause;
cl2 = svmclassify(svmStr2,data(te2,:),'showplot',true);
classperf(cp2,cl2,te2);
cp2.CorrectRate
cl2 = svmclassify(svmStr2,data,'showplot',false);
pause;

% hold on;
figure;
svmStr3 = svmtrain(data(tr3,:),gr3(tr3),'showplot',true,'Kernel_Function','rbf','RBF_Sigma',0.5,'boxconstraint',1000);
cl3 = svmclassify(svmStr3,data(tr3,:),'showplot',false);
classperf(cp3,cl3,tr3);
cp3.CorrectRate
cl3 = svmclassify(svmStr3,data(te3,:),'showplot',true);
classperf(cp3,cl3,te3);
cp3.CorrectRate
cl3 = svmclassify(svmStr3,data,'showplot',false);


%classification results
wynik=[cl1 cl2 cl3];


s=(sum(wynik'))';
wynik=[wynik s]; % 3 first columns - classification results % 
W=te1+te2+te3;
wynik=[wynik W]; % 

%classification results
grupy=zeros(size(data,1),1);
grupy(gr1==1)=1;
grupy(gr2==1)=2;
grupy(gr3==1)=3;
wynik=[grupy wynik]; % first column - into which group the data are classified 
% you should take into consideration, that the the classification
% results are obtained for the whole data set (also for training examples)
% to validate your own tool you should take into consideration only the
% testing data
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%one against all with all the features
clear all;
close all;
load fisheriris;
data = meas; %all avaliable features
gr1 = ismember(species,'setosa');
gr2=ismember(species,'versicolor');
gr3=ismember(species,'virginica');
[tr1, te1] = crossvalind('holdOut',gr1);
cp1 = classperf(gr1);
[tr2, te2] = crossvalind('holdOut',gr2);
cp2 = classperf(gr2);
[tr3, te3] = crossvalind('holdOut',gr3);
cp3 = classperf(gr3);
svmStr1 = svmtrain(data(tr1,:),gr1(tr1),'showplot',true,'Kernel_Function','rbf','RBF_Sigma',0.5,'boxconstraint',1000);
cl1 = svmclassify(svmStr1,data(tr1,:),'showplot',false);
classperf(cp1,cl1,tr1);
cl1 = svmclassify(svmStr1,data(te1,:),'showplot',true);
classperf(cp1,cl1,te1);
cr1=cp1.CorrectRate
cl1 = svmclassify(svmStr1,data,'showplot',false);

% hold on;
svmStr2 = svmtrain(data(tr2,:),gr2(tr2),'showplot',true,'Kernel_Function','rbf','RBF_Sigma',0.5,'boxconstraint',1000);
cl2 = svmclassify(svmStr2,data(tr2,:),'showplot',false);
classperf(cp2,cl2,tr2);
cp2.CorrectRate
cl2 = svmclassify(svmStr2,data(te2,:),'showplot',true);
classperf(cp2,cl2,te2);
cp2.CorrectRate
cl2 = svmclassify(svmStr2,data,'showplot',false);

% hold on;
svmStr3 = svmtrain(data(tr3,:),gr3(tr3),'showplot',true,'Kernel_Function','rbf','RBF_Sigma',0.5,'boxconstraint',1000);
cl3 = svmclassify(svmStr3,data(tr3,:),'showplot',false);
classperf(cp3,cl3,tr3);
cp3.CorrectRate
cl3 = svmclassify(svmStr3,data(te3,:),'showplot',true);
classperf(cp3,cl3,te3);
cp3.CorrectRate
cl3 = svmclassify(svmStr3,data,'showplot',false);

results
wynik1=[cl1 cl2 cl3];
s=(sum(wynik1'))';
wynik1=[wynik1 s];

%results
grupy=zeros(size(data,2),1);
grupy(gr1==1)=1;
grupy(gr2==1)=2;

grupy(gr3==1)=3;
wynik1=[grupy wynik1];
grupa1=species(wynik1(:,2)==1&wynik1(:,5)==1);
grupa2=species(wynik1(:,3)==1&wynik1(:,5)==1);
grupa3=species(wynik1(:,4)==1&wynik1(:,5)==1);
grupano=species(wynik1(:,5)==0);
grupamn=species(wynik1(:,5)==2);