%This program work with the program -- gmm_new.m and is to classify the 10 class training data
clear;
gmm_all;

load test_data;

test_data_separate;

%Initial Variables

class1_size=size(test_class1);
class1_data_no=class1_size(1);
class1_prob=zeros(class1_data_no,10);

class2_size=size(test_class2);
class2_data_no=class2_size(1);
class2_prob=zeros(class2_data_no,10);

class3_size=size(test_class3);
class3_data_no=class3_size(1);
class3_prob=zeros(class3_data_no,10);

class4_size=size(test_class4);
class4_data_no=class4_size(1);
class4_prob=zeros(class4_data_no,10);

class5_size=size(test_class5);
class5_data_no=class5_size(1);
class5_prob=zeros(class5_data_no,10);

class6_size=size(test_class6);
class6_data_no=class6_size(1);
class6_prob=zeros(class6_data_no,10);

class7_size=size(test_class7);
class7_data_no=class7_size(1);
class7_prob=zeros(class7_data_no,10);

class8_size=size(test_class8);
class8_data_no=class8_size(1);
class8_prob=zeros(class8_data_no,10);

class9_size=size(test_class9);
class9_data_no=class9_size(1);
class9_prob=zeros(class9_data_no,10);

class10_size=size(test_class10);
class10_data_no=class10_size(1);
class10_prob=zeros(class10_data_no,10);


class1_corr=0;
class2_corr=0;
class1_incorr=0;
class2_incorr=0;
class1_accur=0;
class2_accur=0;

class3_corr=0;
class4_corr=0;
class3_incorr=0;
class4_incorr=0;
class3_accur=0;
class4_accur=0;

class5_corr=0;
class6_corr=0;
class5_incorr=0;
class6_incorr=0;
class5_accur=0;
class6_accur=0;


class7_corr=0;
class8_corr=0;
class7_incorr=0;
class8_incorr=0;
class7_accur=0;
class8_accur=0;



class9_corr=0;
class10_corr=0;
class9_incorr=0;
class10_incorr=0;
class9_accur=0;
class10_accur=0;




% This is to find the accuracy of the classification and so, we assume the class of the data points were known
% and we can as a result, compute it separately

wrongly_classify=[];
err=1;

%compute the prob. of the data belongs to class 1 and 2
for row=1:class1_data_no
    
    %Compute the prob. that the data belongs to class 1 - 10
    
    
    class1_prob(row,1)=gmmprob(gmm1,test_class1(row,:));
    class1_prob(row,2)=gmmprob(gmm2,test_class1(row,:));
    class1_prob(row,3)=gmmprob(gmm3,test_class1(row,:));
    class1_prob(row,4)=gmmprob(gmm4,test_class1(row,:));
    class1_prob(row,5)=gmmprob(gmm5,test_class1(row,:));
    class1_prob(row,6)=gmmprob(gmm6,test_class1(row,:));
    class1_prob(row,7)=gmmprob(gmm7,test_class1(row,:));
    class1_prob(row,8)=gmmprob(gmm8,test_class1(row,:));
    class1_prob(row,9)=gmmprob(gmm9,test_class1(row,:));
    class1_prob(row,10)=gmmprob(gmm10,test_class1(row,:));
    
    %Check if the classification is correct
    if max(class1_prob(row, :)) ==class1_prob(row, 1)
        class1_corr=class1_corr+1;
    else
        class1_incorr=class1_incorr+1;
        wrongly_classify(err,:)=test_class1(row,:);
        err=err+1;
    end
end



for row=1:class2_data_no
    
    %Compute the prob. that the data belongs to class 1 - 10
    
    
    class2_prob(row,1)=gmmprob(gmm1,test_class2(row,:));
    class2_prob(row,2)=gmmprob(gmm2,test_class2(row,:));
    class2_prob(row,3)=gmmprob(gmm3,test_class2(row,:));
    class2_prob(row,4)=gmmprob(gmm4,test_class2(row,:));
    class2_prob(row,5)=gmmprob(gmm5,test_class2(row,:));
    class2_prob(row,6)=gmmprob(gmm6,test_class2(row,:));
    class2_prob(row,7)=gmmprob(gmm7,test_class2(row,:));
    class2_prob(row,8)=gmmprob(gmm8,test_class2(row,:));
    class2_prob(row,9)=gmmprob(gmm9,test_class2(row,:));
    class2_prob(row,10)=gmmprob(gmm10,test_class2(row,:));
    
    
    if max(class2_prob(row, :)) == class2_prob(row, 2)
        class2_corr=class2_corr+1;
    else
        class2_incorr=class2_incorr+1;
        wrongly_classify(err,:)=test_class2(row,:);
        err=err+1;
        
    end
end


for row=1:class3_data_no
    
    %Compute the prob. that the data belongs to class 1 - 10
    
    
    class3_prob(row,1)=gmmprob(gmm1,test_class3(row,:));
    class3_prob(row,2)=gmmprob(gmm2,test_class3(row,:));
    class3_prob(row,3)=gmmprob(gmm3,test_class3(row,:));
    class3_prob(row,4)=gmmprob(gmm4,test_class3(row,:));
    class3_prob(row,5)=gmmprob(gmm5,test_class3(row,:));
    class3_prob(row,6)=gmmprob(gmm6,test_class3(row,:));
    class3_prob(row,7)=gmmprob(gmm7,test_class3(row,:));
    class3_prob(row,8)=gmmprob(gmm8,test_class3(row,:));
    class3_prob(row,9)=gmmprob(gmm9,test_class3(row,:));
    class3_prob(row,10)=gmmprob(gmm10,test_class3(row,:));
    
    
    if max(class3_prob(row, :)) == class3_prob(row, 3)
        class3_corr=class3_corr+1;
    else
        class3_incorr=class3_incorr+1;
        wrongly_classify(err,:)=test_class3(row,:);
        err=err+1;
        
    end
           
end



for row=1:class4_data_no
    
    %Compute the prob. that the data belongs to class 1 - 10
    
    
    class4_prob(row,1)=gmmprob(gmm1,test_class4(row,:));
    class4_prob(row,2)=gmmprob(gmm2,test_class4(row,:));
    class4_prob(row,3)=gmmprob(gmm3,test_class4(row,:));
    class4_prob(row,4)=gmmprob(gmm4,test_class4(row,:));
    class4_prob(row,5)=gmmprob(gmm5,test_class4(row,:));
    class4_prob(row,6)=gmmprob(gmm6,test_class4(row,:));
    class4_prob(row,7)=gmmprob(gmm7,test_class4(row,:));
    class4_prob(row,8)=gmmprob(gmm8,test_class4(row,:));
    class4_prob(row,9)=gmmprob(gmm9,test_class4(row,:));
    class4_prob(row,10)=gmmprob(gmm10,test_class4(row,:));
    
    
    if max(class4_prob(row, :)) == class4_prob(row, 4)
        class4_corr=class4_corr+1;
    else
        class4_incorr=class4_incorr+1;
        wrongly_classify(err,:)=test_class4(row,:);
        err=err+1;
        
        
     end
           
 end


 for row=1:class5_data_no
    
    %Compute the prob. that the data belongs to class 1 - 10
    
    
    class5_prob(row,1)=gmmprob(gmm1,test_class5(row,:));
    class5_prob(row,2)=gmmprob(gmm2,test_class5(row,:));
    class5_prob(row,3)=gmmprob(gmm3,test_class5(row,:));
    class5_prob(row,4)=gmmprob(gmm4,test_class5(row,:));
    class5_prob(row,5)=gmmprob(gmm5,test_class5(row,:));
    class5_prob(row,6)=gmmprob(gmm6,test_class5(row,:));
    class5_prob(row,7)=gmmprob(gmm7,test_class5(row,:));
    class5_prob(row,8)=gmmprob(gmm8,test_class5(row,:));
    class5_prob(row,9)=gmmprob(gmm9,test_class5(row,:));
    class5_prob(row,10)=gmmprob(gmm10,test_class5(row,:));
    
    
    if max(class5_prob(row, :)) == class5_prob(row, 5)
        class5_corr=class5_corr+1;
    else
        class5_incorr=class5_incorr+1;
        wrongly_classify(err,:)=test_class5(row,:);
        err=err+1;
        
        
    end
           
end
 

 for row=1:class6_data_no
    
    %Compute the prob. that the data belongs to class 1 - 10
    
    
    class6_prob(row,1)=gmmprob(gmm1,test_class6(row,:));
    class6_prob(row,2)=gmmprob(gmm2,test_class6(row,:));
    class6_prob(row,3)=gmmprob(gmm3,test_class6(row,:));
    class6_prob(row,4)=gmmprob(gmm4,test_class6(row,:));
    class6_prob(row,5)=gmmprob(gmm5,test_class6(row,:));
    class6_prob(row,6)=gmmprob(gmm6,test_class6(row,:));
    class6_prob(row,7)=gmmprob(gmm7,test_class6(row,:));
    class6_prob(row,8)=gmmprob(gmm8,test_class6(row,:));
    class6_prob(row,9)=gmmprob(gmm9,test_class6(row,:));
    class6_prob(row,10)=gmmprob(gmm10,test_class6(row,:));
    
    
    if max(class6_prob(row, :)) == class6_prob(row, 6)
        class6_corr=class6_corr+1;
    else
        class6_incorr=class6_incorr+1;
        wrongly_classify(err,:)=test_class6(row,:);
        err=err+1;
        
        
     end
           
 end


  for row=1:class7_data_no
    
    %Compute the prob. that the data belongs to class 1 - 10
    
    
    class7_prob(row,1)=gmmprob(gmm1,test_class7(row,:));
    class7_prob(row,2)=gmmprob(gmm2,test_class7(row,:));
    class7_prob(row,3)=gmmprob(gmm3,test_class7(row,:));
    class7_prob(row,4)=gmmprob(gmm4,test_class7(row,:));
    class7_prob(row,5)=gmmprob(gmm5,test_class7(row,:));
    class7_prob(row,6)=gmmprob(gmm6,test_class7(row,:));
    class7_prob(row,7)=gmmprob(gmm7,test_class7(row,:));
    class7_prob(row,8)=gmmprob(gmm8,test_class7(row,:));
    class7_prob(row,9)=gmmprob(gmm9,test_class7(row,:));
    class7_prob(row,10)=gmmprob(gmm10,test_class7(row,:));
    
    
    if max(class7_prob(row, :) )== class7_prob(row, 7)
        class7_corr=class7_corr+1;
    else
        class7_incorr=class7_incorr+1;
        wrongly_classify(err,:)=test_class7(row,:);
        err=err+1;
        
        
      end
           
  end
 
   for row=1:class8_data_no
    
    %Compute the prob. that the data belongs to class 1 - 10
    
    
    class8_prob(row,1)=gmmprob(gmm1,test_class8(row,:));
    class8_prob(row,2)=gmmprob(gmm2,test_class8(row,:));
    class8_prob(row,3)=gmmprob(gmm3,test_class8(row,:));
    class8_prob(row,4)=gmmprob(gmm4,test_class8(row,:));
    class8_prob(row,5)=gmmprob(gmm5,test_class8(row,:));
    class8_prob(row,6)=gmmprob(gmm6,test_class8(row,:));
    class8_prob(row,7)=gmmprob(gmm7,test_class8(row,:));
    class8_prob(row,8)=gmmprob(gmm8,test_class8(row,:));
    class8_prob(row,9)=gmmprob(gmm9,test_class8(row,:));
    class8_prob(row,10)=gmmprob(gmm10,test_class8(row,:));
    
    
    if max(class8_prob(row, :)) == class8_prob(row, 8)
        class8_corr=class8_corr+1;
    else
        class8_incorr=class8_incorr+1;
        wrongly_classify(err,:)=test_class8(row,:);
        err=err+1;
        
        
       end
           
 end
 
 
for row=1:class9_data_no
    
    %Compute the prob. that the data belongs to class 1 - 10
    
    
    class9_prob(row,1)=gmmprob(gmm1,test_class9(row,:));
    class9_prob(row,2)=gmmprob(gmm2,test_class9(row,:));
    class9_prob(row,3)=gmmprob(gmm3,test_class9(row,:));
    class9_prob(row,4)=gmmprob(gmm4,test_class9(row,:));
    class9_prob(row,5)=gmmprob(gmm5,test_class9(row,:));
    class9_prob(row,6)=gmmprob(gmm6,test_class9(row,:));
    class9_prob(row,7)=gmmprob(gmm7,test_class9(row,:));
    class9_prob(row,8)=gmmprob(gmm8,test_class9(row,:));
    class9_prob(row,9)=gmmprob(gmm9,test_class9(row,:));
    class9_prob(row,10)=gmmprob(gmm10,test_class9(row,:));
    
    
    if max(class9_prob(row, :)) == class9_prob(row, 9)
        class9_corr=class9_corr+1;
    else
        class9_incorr=class9_incorr+1;
        wrongly_classify(err,:)=test_class9(row,:);
        err=err+1;
        
        
     end
           
end
 

for row=1:class10_data_no
    
    %Compute the prob. that the data belongs to class 1 - 10
    
    
    class10_prob(row,1)=gmmprob(gmm1,test_class10(row,:));
    class10_prob(row,2)=gmmprob(gmm2,test_class10(row,:));
    class10_prob(row,3)=gmmprob(gmm3,test_class10(row,:));
    class10_prob(row,4)=gmmprob(gmm4,test_class10(row,:));
    class10_prob(row,5)=gmmprob(gmm5,test_class10(row,:));
    class10_prob(row,6)=gmmprob(gmm6,test_class10(row,:));
    class10_prob(row,7)=gmmprob(gmm7,test_class10(row,:));
    class10_prob(row,8)=gmmprob(gmm8,test_class10(row,:));
    class10_prob(row,9)=gmmprob(gmm9,test_class10(row,:));
    class10_prob(row,10)=gmmprob(gmm10,test_class10(row,:));
    
    
    if max(class10_prob(row, :)) == class10_prob(row, 10)
        class10_corr=class10_corr+1;
    else
        class10_incorr=class10_incorr+1;
        wrongly_classify(err,:)=test_class10(row,:);
        err=err+1;
        
        
     end 
           
end 


%compute the accuracy of each classes

class1_accur=(class1_corr./class1_data_no).*100;
class2_accur=(class2_corr./class2_data_no).*100;

class3_accur=(class3_corr./class3_data_no).*100;
class4_accur=(class4_corr./class4_data_no).*100;

class5_accur=(class5_corr./class5_data_no).*100;
class6_accur=(class6_corr./class6_data_no).*100;

class7_accur=(class7_corr./class7_data_no).*100;
class8_accur=(class8_corr./class8_data_no).*100;

class9_accur=(class9_corr./class9_data_no).*100;
class10_accur=(class10_corr./class10_data_no).*100;

%Compute overall accuracy

overall_accuracy=0;
overall_accuracy=(1-((err./333))).*100

%Plot the testing data

figure, plot(test_class1(:, 1), test_class1(:, 2), 'ro' ,test_class2(:, 1), test_class2(:, 2), 'go', test_class3(:, 1), test_class3(:, 2), 'bo' , test_class4(:, 1), test_class4(:, 2), 'mo' , test_class5(:, 1), test_class5(:, 2), 'co' , test_class6(:, 1), test_class6(:, 2), 'bs' , test_class7(:, 1), test_class7(:, 2), 'cs' , test_class8(:, 1), test_class8(:, 2), 'rs' ,  test_class9(:, 1), test_class9(:, 2), 'ms' , test_class10(:, 1), test_class10(:, 2), 'gs' );                                        
legend('Class 1', 'Class 2' , 'Calss 3' , 'Class 4' , 'Class 5' , 'Class 6' , 'Class 7' , ' Class 8' , 'Class 9' , 'Class 10' , -1)
title('The testing data')

%Plot the training data

figure, plot(import_class1(:, 1), import_class1(:, 2), 'ro' ,import_class2(:, 1), import_class2(:, 2), 'go', import_class3(:, 1), import_class3(:, 2), 'bo' , import_class4(:, 1), import_class4(:, 2), 'mo' , import_class5(:, 1), import_class5(:, 2), 'co' , import_class6(:, 1), import_class6(:, 2), 'bs' , import_class7(:, 1), import_class7(:, 2), 'cs' , import_class8(:, 1), import_class8(:, 2), 'rs' ,  import_class9(:, 1), import_class9(:, 2), 'ms' , import_class10(:, 1), import_class10(:, 2), 'gs' );                                        
legend('Class 1', 'Class 2' , 'Calss 3' , 'Class 4' , 'Class 5' , 'Class 6' , 'Class 7' , ' Class 8' , 'Class 9' , 'Class 10' , -1)
title('The training data')


%Plot the results
figure, plot(test_class1(:, 1), test_class1(:, 2), 'ro' ,test_class2(:, 1), test_class2(:, 2), 'go', test_class3(:, 1), test_class3(:, 2), 'bo' , test_class4(:, 1), test_class4(:, 2), 'mo' , test_class5(:, 1), test_class5(:, 2), 'co' , test_class6(:, 1), test_class6(:, 2), 'bs' , test_class7(:, 1), test_class7(:, 2), 'cs' , test_class8(:, 1), test_class8(:, 2), 'rs' ,  test_class9(:, 1), test_class9(:, 2), 'ms' , test_class10(:, 1), test_class10(:, 2), 'gs' ,wrongly_classify(:,1),wrongly_classify(:,2), 'kx');                                        
legend('Class 1', 'Class 2' , 'Calss 3' , 'Class 4' , 'Class 5' , 'Class 6' , 'Class 7' , ' Class 8' , 'Class 9' , 'Class 10' ,'Wrong', -1)

%figure, plot(import_class1(:, 1), import_class1(:, 2), 'ro' ,import_class2(:, 1), import_class2(:, 2), 'go', wrongly_classify(:,1), wrongly_classify(:,2), 'kx');
%legend('Class 1', 'Class 2' , 'Wrongly Classified' , 2)
title('Data which classified wrongly!');


