Tuesday, March 5, 2013

Support Vector Machines in MATLAB II

%non-linear 2-class SVM example
var = 1.25;
n = 100; mu1 = [0; 0]; cov1 = [1 .8; 1 .8];
x1 = repmat(mu1',n/2,1) + randn(n/2,2)*chol(cov1)*sqrt(var);
xs2 = repmat(mu1',n*100,1) + randn(n*100,2)*sqrt(var*3);
ind = sum((xs2.^2)')' > 5;
x2 = xs2(ind,:);
x2 = x2(1:(n/2),:);
x = [x1; x2];
Y = [repmat(1,50,1); repmat(-1,50,1)];
clf
hold on
scatter(x1(:,1),x1(:,2),'or','filled')
scatter(x2(:,1),x2(:,2),'ob','filled')
axis([min(min(x(:,1))) max(max(x(:,1))) min(min(x(:,2))) max(max(x(:,2)))]);
hold off



% linear kernel
C = 1;
svms = svmtrain(x,Y,'kernel_function','linear','showplot','true','method','QP','boxconstraint',C);


% radial kernel
sig = .75;
C = 1;
svms = svmtrain(x,Y,'kernel_function','rbf','showplot','true','method','QP','rbf_sigma',sig,'boxconstraint',C);


% polynomial kernel
d = 2;
C = 1;
svms = svmtrain(x,Y,'kernel_function','polynomial','showplot','true','method','QP','polyorder',d,'boxconstraint',C);

No comments:

Post a Comment