clc
clear
close all 

% Load the data set

%path_data='/Users/mohamedbennasar/Documents/My teaching stuff/AI/MATLAB/data_pre/creditcard.csv';
%path_data='D:\Dara_Pre/pima-indians-diabetes.csv';
%path_data='/Users/mohamedbennasar/Documents/My teaching stuff/AI/MATLAB/data_pre/glass.csv';
path_data='D:\Dara_Pre\haberman.csv';


T = readtable(path_data);
Data_set = table2array(T); % convert table to array 
[m,n]=size(Data_set);
%% Find the number of classes 

[cnt_unique, unique_C] = hist(Data_set(:,n),unique(Data_set(:,n)));

bar(cnt_unique);

%% Cost matrix

 % creat the cost matrix based on the ration of the classes 
   
C=zeros(length(cnt_unique), length(cnt_unique));

for i=1:length(cnt_unique)
    for j=1:length(cnt_unique)
        if i ~= j
            C(i,j)=cnt_unique(j)/cnt_unique(i);
        end
    end
end

C=ceil(C); %Convert the values in the matrix into integer



%% Classification 



Temp=Data_set(:,1:end-1);
Class=Data_set(:,n);
folds=5;
cp_WITH = classperf(Class);
cp_WITHOUT = classperf(Class);
Predicted_Class=zeros(m,2);

indices = crossvalind('Kfold',Class(:),folds);
        
 for i = 1:folds
        clear nbSVM 
       test = (indices == i); train = ~test;
     
        Training_Features=Temp(train,:);
        Testfeatures=Temp(test,:);
        Y=Class(logical(train));
        

      
      % Train the classifier WITHOUT the cost matrix 
       t_WITHOUT = templateSVM('KernelFunction','rbf','KernelScale', 'auto', 'Standardize', 1);
       nb_WITHOUT = fitcecoc(Training_Features,Y,'Learners',t_WITHOUT);
                 
          Predicted_ac_WITHOUT =predict(nb_WITHOUT,Testfeatures);
          classperf(cp_WITHOUT,Predicted_ac_WITHOUT,test);
         Predicted_Class(test,1)=Predicted_ac_WITHOUT;
       
       
       
       % Train the classifier WITH the cost matrix 
        t_WITH= templateSVM('KernelFunction','rbf','KernelScale', 'auto', 'Standardize', 1,'Cost',C);
        
          nb_WITH = fitcecoc(Training_Features,Y,'Learners',t_WITH);
                 
          Predicted_ac_WITH =predict(nb_WITH,Testfeatures);
          classperf(cp_WITH,Predicted_ac_WITH,test);
         Predicted_Class(test,2)=Predicted_ac_WITH;
          
  end    
    
         
     message = sprintf('The C_Acuracy: %.2f, PositivePredictiveValue:%.2f,NegativePredictiveValue:%.2f', cp_WITHOUT.CorrectRate,cp_WITHOUT.PositivePredictiveValue,cp_WITHOUT.NegativePredictiveValue);
     msgbox(message);

figure
cm_cp_WITHOUT = confusionchart(Data_set(:,n),Predicted_Class(:,1), ...
    'Title','WITHOUT', ...
    'RowSummary','row-normalized', ...
    'ColumnSummary','column-normalized');

         
     message = sprintf('The C_Acuracy: %.2f, PositivePredictiveValue:%.2f,NegativePredictiveValue:%.2f', cp_WITH.CorrectRate,cp_WITH.PositivePredictiveValue,cp_WITH.NegativePredictiveValue);
     msgbox(message);

figure
cm_WITH = confusionchart(Data_set(:,n),Predicted_Class(:,2), ...
    'Title','WITH', ...
    'RowSummary','row-normalized', ...
    'ColumnSummary','column-normalized');

  %% OVER SAMPLING ( SMOTE)
  
  addpath('D:\Dara_Pre\');
 % Using SMOTE method to generate synthetic datapoint from the minority class
  [X,C] = smote(Data_set(:,1:n-1), [], 'Class', Data_set(:,n));
   hist(C)
   
 
%% Classification 



Temp=X(:,1:end-1);
Class=C;
folds=5;
cp = classperf(C);


indices = crossvalind('Kfold',Class(:),folds);
        
 for i = 1:folds
        clear nbSVM 
       test = (indices == i); train = ~test;
     
        Training_Features=Temp(train,:);
        Testfeatures=Temp(test,:);
        Y=Class(logical(train));
        

       % nb=fitcknn(Training_Features,Y,'NumNeighbors',3,'NSMethod','exhaustive','Distance','minkowski', 'Standardize',1);
       
       t = templateSVM('KernelFunction','rbf','KernelScale', 'auto', 'Standardize', 1);
        % t= templateSVM('KernelFunction','rbf','KernelScale', 'auto', 'Standardize', 1,'Cost',C);
        
          nb = fitcecoc(Training_Features,Y,'Learners',t);
                 
          Predicted_ac =predict(nb,Testfeatures);
          classperf(cp,Predicted_ac,test);
         Predicted_Class_S(test)=Predicted_ac;
          
  end    
    cp.CorrectRate;
     
     
     
     message = sprintf('The C_Acuracy: %.2f, PositivePredictiveValue:%.2f,NegativePredictiveValue:%.2f', cp.CorrectRate,cp.PositivePredictiveValue,cp.NegativePredictiveValue);
     msgbox(message);

figure
cm = confusionchart(C(:),Predicted_Class_S(:), ...
    'Title','SMOTE', ...
    'RowSummary','row-normalized', ...
    'ColumnSummary','column-normalized');   
     

     
      