SVM 分类器
最近在学习Machine learning,尝试自己用Matlab实现SVM算法svm基本思路是让分界的margin尽量大,可以转化为一个standard quadratic programming problem,这样在matlab中有就可以调用现成的函数解决
Code
%svm demo
%this m file can be used to solve unseparable problem
clear;
close all;
clc;
%positive points numbers
Np = 15 ;
%negative points numbers
Nn = 15 ;
%generate data
= gendata(Np, Nn) ;
tol = 1e-4 ;
%% build matrix for quadprog()
= size(x); %m: number of train data; n: length of xi
Y = diag(y);
%%primal formulation
%linear kernel
K = x'*x;
C = 1;
H = Y*K*Y;
f = -ones(m, 1);
A = [];
b = [];
Aeq = y;
beq = 0;
l = zeros(m, 1);
u = 1*ones(m, 1);
options = optimset('Algorithm','interior-point-convex');
%%standard quadratic programming problem X = quadprog(H,f,A,b,Aeq,beq,LB,UB,X0)
alpha = quadprog(H, f, A, b, Aeq, beq, l, u, [], options);
%%compute w
w = zeros(n, 1);
for i = 1 : m
w = w + alpha(i)*y(i)*x(:, i);
end
%%compute b
pos = find(alpha > 1e-6);
b = mean(y(pos)- w'*x(:, pos));
%%plot the boundry on the map
xt = linspace(min(x(1, :)), max(x(2, :)), 100);
yt = (-b-w(1)*xt)/w(2);
yt_p = (1 -b -w(1)*xt)/w(2);
yt_n = (-1 -b - w(1)*xt)/w(2);
%%plot data
xp = x(:, y == 1);
xn = x(:, y == -1);
figure;
plot(xp(1, :), xp(2, :), 'ob', xn(1, :), xn(2, :), '*r');
hold on;
plot(xt, yt, '-r', xt, yt_n, '-b', xt, yt_p, '-b');
hold off;
print('-dpng', 'svm_soft_margin_simu.png')
占个楼先 自己占个楼先 MATLAB已经有现成的SVM分类器可以用了。
LIBSVM是也不错,国内用的较多。 RUANJI 发表于 2015-1-9 22:20
MATLAB已经有现成的SVM分类器可以用了。
LIBSVM是也不错,国内用的较多。
还是感觉用自己写的库最靠谱
页:
[1]