clear all;

data = load('Data/iris.txt');

% Calculate numbers of examples and features
[n_examples, data_col] = size(data)
n_features = data_col - 1

% Divide class and features
class=data(:,1);
features=data(:,2:end);
class1 = find(class==1);
class2 = find(class==2);
class3 = find(class==3);

% Plot pairs of feature for 3 different classes
figure(1)
plot(features(class1,1),features(class1,2),'*',features(class2,1),features(class2,2),'o',...
    features(class3,1),features(class3,2),'+');
title('Sepal length and width.')
figure(2)
plot(features(class1,3),features(class1,4),'*',features(class2,3),features(class2,4),'o',...
    features(class3,3),features(class3,4),'+');
title('Petal length and width.')

% From the graphs we see that 2nd and 3rd classes are not lineary separable; 
% We will try to solve two class classification problem where we combine
% 2nd and 3rd classes
class(class1,:)=0;
class(class2,:)=1;
class(class3,:)=1;

data = [features class];

for run=1:100 % run classification 100 times to get average results
    % split the data into training  and testing subsets
    % leave 30% of data for testing of our model
    [train, testin] = divideset(data, 30);
    % normalize all the features to mean 0 and variance 1;
    [meanv, stdv, train(:, 1 : n_features)] = normalize(train(:, 1 : n_features), [], []);
    % normalize testing data
    [meanv, stdv, testin(:, 1 : n_features)] = normalize(testin(:, 1 : n_features), [], []);
    
    % set learning rate
    alpha = 0.1;
    % set the limit on number of epochs when the weights do not change
    epochs_limit=15;
    % train perceptron
    W = perceptron(train(:, 1 : n_features), train(:, n_features+1:end), alpha, epochs_limit);
    
    % test on 30%
    % add bias to the testin
    n_test_examples = size(testin,1);
    testin = [ones(n_test_examples,1) testin];
    testout = ((W * testin(:,1:n_features+1)')>=0);
    testtgt = testin(:,end);
    
    % Calculate the accuracy and confidence matrix
    [acc(run,1), conf_matrix] = accuracy(testout',testtgt);
    % mean square error
    err(run,1) = sum((testout'-testtgt).^2)/size(testtgt,1);
end
% average accuracy 0.92, the max is 1
mean(acc)
max(acc)

return