%%%This is a Matlab Demo of Decision Trees%%%%%%%%
%%%Author: Avirup Sil
%%%Temple University
%%%Wachman Hall 318
%%%Email: avirup.sil@temple.edu
%%%Done on the Fisher's Iris data
%%%Data has been divided randomly into 50% training
%%%and the other 50% left as Unseen or Test Data
%%%Accuracy obtained is 92% on Test Data.


%%
%%%%%%%%%%Iris Dataset%%%%%%%%%%%%%%%%%%%%%%%%
disp('Training data is being loaded..Please Wait!');
 [data, target_train] = xlsread('Data/iris_train.csv',1);
 load('Data/target_train');
 disp('Training Data loaded successfully');
 yorig = target_train;
 meas = data;
 species = target_train;
%%
%%Create the tree...
disp('PRINTING THE RULES EXTRACTED FROM TRAINING DATA...');

%Main function call to classregtree
 t = classregtree(meas,species,...
'method','classification',...
'names',{'SL' 'SW' 'PL' 'PW'},...
'minparent',1,'minleaf',1)
disp('PRINTING DECISION TREE....See figure not command window');

%View the tree
 view(t)
 %%
 %%Testing the model with test data
 disp('Test data is being loaded..Please Wait!');
 [test, target_test] = xlsread('Data/iris_test.csv',1);
 yorig = target_test(:,5);
 ypred = eval(t,test);
 %So now, yorig has actual values and ypred has the 
 %values which are predicted by the decision tree model
 
disp('Following is the confusion matrix');
cm = confusionmat(yorig,ypred)
N = sum(cm(:));
err = ( N-sum(diag(cm)) ) / N; 
disp('The Accuracy of our Model wrt Test Data is');
Accuracy = 1 - err

%%%%%%%%%%%%%Done%%%%%%%%%%%%%%%%%%%%%%%%%%%%%