clear;
irisData = dlmread('iris.data');
plotAxis = [min(irisData(:, 3)) max(irisData(:, 3)) min(irisData(:, 4)) max(irisData(:, 4))];
kernel = 'rbf';
c = [1 10 100 1000 10000];
sigma = 1 : 0.5: 3;
[C, Sigma] = meshgrid(c, sigma);
C = C(:);
Sigma = Sigma(:);
Err = zeros(size(C));
n = length(Err);
Xtest = irisData(1 : 150, 3 : 4);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% First train Isis-setosa(1) and Isis-versicolor(2) using grid search.
%
disp('.....................................................................')
X = irisData(1 : 100, 3 : 4);
Y = irisData(1 : 100, 5);
Y(find(Y==1)) = 1;
Y(find(Y==2)) = -1;
Xtrain = X;
disp('Training Isis-setosa(1) and Isis-versicolor(2) using grid search.')
disp('Please wait a while.');
for i = 1 : n
[alphaStar, bStar, SVIndex] = yxcSVMtrain(X, Y, C(i), kernel, Sigma(i));
[YClassified, Z, Err(i)] = yxcSVMclassifer(Xtrain, Xtrain, Y, alphaStar, bStar, kernel, Sigma(i));
end
[mErr, i] = min(Err);
%Compute the minimum one again, using the optimal C and Sigma.
[alphaStar, bStar, SVIndex] = yxcSVMtrain(X, Y, C(i), kernel, Sigma(i));
[YClassified, Z, Err(i)] = yxcSVMclassifer(Xtrain, Xtrain, Y, alphaStar, bStar, kernel, Sigma(i));
disp(sprintf('Error rate is %%%d, with C = %d, sigma = %d', mErr*100, C(i), Sigma(i)));
disp(sprintf('There are %d support vectors, %d out of %d are wrongly classified', ...
length(SVIndex), mErr*length(Y), length(Y)));
yxcSVMplot(X, Y, SVIndex, alphaStar, bStar, kernel, Sigma(i), plotAxis);
title(sprintf('Isis-setosa(1) and Isis-versicolor(2), SV = %d, Err = %%%d, C = %d, Sigma = %d', ...
length(SVIndex), mErr*100, C(i), Sigma(i)));
% Test all data on the Isis-setosa(1) and Isis-versicolor(2) data set.
[Y12, Z] = yxcSVMclassifer(Xtrain, Xtest, Y, alphaStar, bStar, kernel, Sigma(i));
Y12(find(Y12==1)) = 1;
Y12(find(Y12==-1)) = 2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Then train Isis-versicolor(2) and Isis-virginica(3) using grid search.
%
disp('.....................................................................')
X = irisData(51 : 150, 3 : 4);
Y = irisData(51 : 150, 5);
Y(find(Y==2)) = 1;
Y(find(Y==3)) = -1;
Xtrain = X;
disp('Training Isis-versicolor(2) and Isis-virginica(3) using grid search.')
disp('Please wait a while.');
for i = 1 : n
[alphaStar, bStar, SVIndex] = yxcSVMtrain(X, Y, C(i), kernel, Sigma(i));
[YClassified, Z, Err(i)] = yxcSVMclassifer(Xtrain, Xtrain, Y, alphaStar, bStar, kernel, Sigma(i));
%disp(sprintf('Cross Validation: %d in %d', i, n));
end
[mErr, i] = min(Err);
%Compute the minimum one again, using the optimal C and Sigma.
[alphaStar, bStar, SVIndex] = yxcSVMtrain(X, Y, C(i), kernel, Sigma(i));
[YClassified, Z, Err(i)] = yxcSVMclassifer(Xtrain, Xtrain, Y, alphaStar, bStar, kernel, Sigma(i));
disp(sprintf('Error rate is %%%d, with C = %d, sigma = %d', mErr*100, C(i), Sigma(i)));
disp(sprintf('There are %d support vectors, %d out of %d are wrongly classified', ...
length(SVIndex), mErr*length(Y), length(Y)));
yxcSVMplot(X, Y, SVIndex, alphaStar, bStar, kernel, Sigma(i), plotAxis);
title(sprintf('Isis-versicolor(2) and Isis-virginica(3), SV = %d, Err = %%%d, C = %d, Sigma = %d', ...
length(SVIndex), mErr*100, C(i), Sigma(i)));
% Test all data on the Isis-versicolor(2) and Isis-virginica(3) data set.
[Y23, Z] = yxcSVMclassifer(Xtrain, Xtest, Y, alphaStar, bStar, kernel, Sigma(i));
Y23(find(Y23==1)) = 2;
Y23(find(Y23==-1)) = 3;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Last train Isis-setosa(1) and Isis-virginica(3) using grid search.
%
disp('.....................................................................')
X = irisData([1:50 101 : 150], 3 : 4);
Y = irisData([1:50 101 : 150], 5);
Y(find(Y==1)) = 1;
Y(find(Y==3)) = -1;
Xtrain = X;
disp('Training Isis-setosa(1) and Isis-virginica(3) using grid search.')
disp('Please wait a while.');
for i = 1 : n
[alphaStar, bStar, SVIndex] = yxcSVMtrain(X, Y, C(i), kernel, Sigma(i));
[YClassified, Z, Err(i)] = yxcSVMclassifer(Xtrain, Xtrain, Y, alphaStar, bStar, kernel, Sigma(i));
%disp(sprintf('Cross Validation: %d in %d', i, n));
end
[mErr, i] = min(Err);
%Compute the minimum one again, using the optimal C and Sigma.
[alphaStar, bStar, SVIndex] = yxcSVMtrain(X, Y, C(i), kernel, Sigma(i));
[YClassified, Z, Err(i)] = yxcSVMclassifer(Xtrain, Xtrain, Y, alphaStar, bStar, kernel, Sigma(i));
disp(sprintf('Error rate is %%%d, with C = %d, sigma = %d', mErr*100, C(i), Sigma(i)));
disp(sprintf('There are %d support vectors, %d out of %d are wrongly classified', ...
length(SVIndex), mErr*length(Y), length(Y)));
yxcSVMplot(X, Y, SVIndex, alphaStar, bStar, kernel, Sigma(i), plotAxis);
title(sprintf('Isis-setosa(1) and Isis-virginica(3), SV = %d, Err = %%%d, C = %d, Sigma = %d', ...
length(SVIndex), mErr*100, C(i), Sigma(i)));
% Test all data on the Isis-versicolor(2) and Isis-virginica(3) data set.
[Y13, Z] = yxcSVMclassifer(Xtrain, Xtest, Y, alphaStar, bStar, kernel, Sigma(i));
Y13(find(Y13==1)) = 1;
Y13(find(Y13==-1)) = 3;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% We use a little trick to find out which class the points belong to
Y123 = sort([Y12 Y23 Y13], 2);
Y123 = Y123(:, 2);
% Plot the classified data.
iY1 = find(Y123 == 1);
iY2 = find(Y123 == 2);
iY3 = find(Y123 == 3);
x = irisData(:, 3);
y = irisData(:, 4);
figure
plot(x(iY1), y(iY1), 'g+');
axis(plotAxis);
hold on;
plot(x(iY2), y(iY2), 'r*');
hold on;
plot(x(iY3), y(iY3), 'y.');
hold on;
% Find out the wrongly classified data.
Y = irisData(:, 5);
iYwrong = find(Y123 ~= Y);
plot(x(iYwrong), y(iYwrong), 'ko');
legend('Isis-setosa(1)', 'Isis-versicolor(2)', 'Isis-virginica(3)', 'Wrong ones',...
'Location', 'NorthWest');
title('The Final Job.');
hold off;