% Kristiyan Georgiev
% A.I. , 12 Oct 2010
% bayesian classifier, using Matlab StatToolBox - NaiveBayes
clear; clf;

%makes it easier, in case you didn't add all folders to the path
addpath('./tools')

%Fisher's iris data consists of measurements on the sepal length, sepal
%width, petal length, and petal width for 150 iris specimens. There are 50
%specimens from each of three species. Load the data and see how the sepal
%measurements differ between species. You can use the two columns containing
%sepal measurements.
%load fisheriris;  % labels are strings, it requires preprocessing
data = load('Data/iris.txt');

%Suppose you measure a sepal and petal from an iris, and you need to
%determine its species on the basis of those measurements
%plot feature1 vs feature2, colored according to the corresponding class
gscatter(data(:,2), data(:,3), data(:,1),'rgb','osd');
xlabel('Sepal length');
ylabel('Sepal width');

% separate 30% of the data for testing data and 70% for training
[train, testin] = divideset(data , 30);

% prepare the input bayesian classifier
trainLabel = train(:,1);
trainData  = train(:,2:end);
testLabel  = testin(:,1);
testData   = testin(:,2:end);
testSize   = size(testLabel,1);

% Create Naive Bayes classifier object by fitting training data
nbKD= NaiveBayes.fit(trainData, trainLabel);
% test the the NaiveBayes Classifier agains the test data
nbKDClass= nbKD.predict(testData);
correctN = sum(nbKDClass == testLabel);
nbKDResubErr = 1- correctN / testSize

% plot the gausian over the scater plot of feature_1 vs feature_2
fprintf('Class\tFeature\tMean\tStd\n');
for c=1:nbKD.NClasses
    g1 = nbKD.Params{c,1}; % dimention 1 of class c
    g2 = nbKD.Params{c,2}; % dimention 2 of class c
    
    % plot gauss2D of feature 1 vs feature 2, for class c 1 std from the
    % mean 
    hold on;  plotgauss2D(g1(1), g1(2), g2(1), g2(2), 1) 
    fprintf('%d\t\t%d\t\t%4.2f\t%4.2f\n', c, 1, g1(1), g1(2));
    fprintf('%d\t\t%d\t\t%4.2f\t%4.2f\n', c, 2, g2(1), g2(2));    
end
fprintf('\n');
axis equal; 

%The plane is divided into regions using lines, such that
%different regions  are assigned to different species.
%I visualize the regions using grid of (x,y) values and apply the
%classification function to that grid.
figure
[x,y] = meshgrid(4:.1:8,2:.1:4.5);
x = x(:);
y = y(:);
j = classify([x y],data(:,2:3),data(:,1));
gscatter(x,y,j,'rgb','osd')

% run Knn for different K
k = 1:2:20;
fprintf('\nK \t\t Correct \t ErrorRate \n')
for i=1:size(k,2)
    prediction = knn(trainData, trainLabel, testData, testLabel, k(i));
    fprintf('%d \t\t %d \t %f\n', k(i), prediction.correct, prediction.error);
end

%Acknowledgement
% I used materrials and code from
% http://www.mathworks.com/products/statistics/demos.html
