% svm demo 2
close;

figure(1)
d  = gen(toy2d('gaussians','l=100'))
ind1 = find(d.Y==1);
ind2 = find(d.Y==-1);

plot(d.X(ind1,1),d.X(ind1,2),'rx');
hold on;
plot(d.X(ind2,1),d.X(ind2,2),'bo');
title('Training data')
disp('Press any key to continue...')
pause;

figure(2)
svmobj = svm({kernel('rbf',1),'optimizer="andre"'});
% svmobj = svm('optimizer="andre"');
% svmobj.C = 1;
[r,a]=train(svmobj,d);
plot(a);
str = sprintf('Number of SVs: %d',nnz(a.alpha));
%disp(str)
title('Decision boundary...')
disp('Press any key to continue...')
pause;

figure(3)
dtst  = gen(toy2d('gaussians','l=100'));
ind1 = find(dtst.Y==1);
ind2 = find(dtst.Y==-1);

plot(dtst.X(ind1,1),dtst.X(ind1,2),'rx');
hold on;
plot(dtst.X(ind2,1),dtst.X(ind2,2),'bo');
title('Test data')
disp('Press any key to continue...')
pause;

% measure classification error
tst=test(a,dtst)
testerr = length(find(tst.X~=dtst.Y))/length(dtst.Y)
str = sprintf('Test classification error: %f',testerr);
disp(str)
