📄 svmex01.m
字号:
%SVMexample01a.m
%Two Dimension SVM Problem, Two Class and Separable Situation
%
%Method from Christopher J. C. Burges:
%"A Tutorial on Support Vector Machines for Pattern Recognition", page 8
%
% Objective: min "f(A)=||W||^2" ,page 8 line 29;
% Subject to: "Yi*(W*X + b)>=1" , function (12);
%
%
echo off
clear all
close all
%Data:set A with Yi = 1 and B with Yi = -1;
A=[1,5;2,6;3,7;1.5,7.2;2.3,7.8;3.9,8.7]'; % Positive samples
B=[1,1;2,2;3,3;1.9,-0.5;2.2,1;3.6,1.4]'; %Negative samples
X=[A;B]; % All samples
%The Object function, minimaze "f"
%variable x(i) as the w(i) in function (12) by the definition of MATLAB function "CONSTR", optimization variable must use a mnemonics "x"
funf = 'f = x(1)^2+x(2)^2;';
%The constrained function (12), no equality, and MATLAB prescrib no-equality must be "<=0",
%so here take a form "1-Yi*(W*X + b)<=0", and x(3) is variable "b";
fung = 'g = [1-1*(1*x(1)+5*x(2)+x(3)); 1-1*(2*x(1)+6*x(2)+x(3)); 1-1*(3*x(1)+7*x(2)+x(3)); 1-1*(1.5*x(1)+7.2*x(2)+x(3)); 1-1*(2.3*x(1)+7.8*x(2)+x(3)); 1-1*(3.9*x(1)+8.7*x(2)+x(3)); 1+1*(1*x(1)+1*x(2)+x(3)); 1+1*(2*x(1)+2*x(2)+x(3)); 1+1*(3*x(1)+3*x(2)+x(3)); 1+1*(1.9*x(1)+0.5*x(2)+x(3)); 1+1*(2.2*x(1)+1*x(2)+x(3)); 1+1*(3.6*x(1)+1.4*x(2)+x(3))];';
fun = [funf fung];
%Start point should be consistent with the number of variables
x0 = [-0.5 0.6 -0.9];
options = [];
[W, options,L] = constr(fun, x0, options);
%Classfication function y = -x*W(1)/W(2)-W(3)/W(2)
figure(1);
whitebg(1,[0,0.1,0]);
plot([A(1:1,1:6)],[A(2:2,1:6)],'m*');
hold on
plot([B(1:1,1:6)],[B(2:2,1:6)],'c*');
plot([0,5],[-W(3)/W(2),-5*W(1)/W(2)-W(3)/W(2)],'y')
axis([0,5,-1,9])
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -