⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 svmex02.m

📁 数据挖掘的新方法-支持向量机书中算法例子
💻 M
字号:
%SVMexample01a.m
%
%Difference with SVMexample01.m:
%           Take the Largrange Function (16)as object function,so it need more time than SVMexample01.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 9
%	
%  Objective:   min "f(A)=-sum(ai)+sum[sum(ai*yi*xi*aj*yj*xj)]/2" ,function (16)
%  Subject to:  sum{ai*yi}=0 ,function (15);
%					 and ai>=0 for any i, the particular set of constraints C2 (page 9, line14). 
%
%

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,6;2,7;3,8];			% Positive samples
B=[1,1;2,2;3,3;1,0;2,1;3,2];			% Negative samples
X=[A;B];										% All samples



%The Object function, minimaze "-f"
%variable x(i) as the ai in function (16) by the definition of MATLAB function "CONSTR", optimization variable must use a mnemonics "x"
funf = 'f=-(x(1)+x(2)+x(3)+x(4)+x(5)+x(6)+x(7)+x(8)+x(9)+x(10)+x(11)+x(12))+0.5*(26*x(1)*x(1)+64*x(1)*x(2)+76*x(1)*x(3)+62*x(1)*x(4)+74*x(1)*x(5)+86*x(1)*x(6)-12*x(1)*x(7)-24*x(1)*x(8)-36*x(1)*x(9)-2*x(1)*x(10)-14*x(1)*x(11)-26*x(1)*x(12)+40*x(2)*x(2)+96*x(2)*x(3)+76*x(2)*x(4)+92*x(2)*x(5)+108*x(2)*x(6)-16*x(2)*x(7)-32*x(2)*x(8)-48*x(2)*x(9)-4*x(2)*x(10)-20*x(2)*x(11)-36*x(2)*x(12)+58*x(3)*x(3)+90*x(3)*x(4)+110*x(3)*x(5)+130*x(3)*x(6)-20*x(3)*x(7)-40*x(3)*x(8)-60*x(3)*x(9)-6*x(3)*x(10)-26*x(3)*x(11)-46*x(3)*x(12)+37*x(4)*x(4)+88*x(4)*x(5)+102*x(4)*x(6)-14*x(4)*x(7)-28*x(4)*x(8)-42*x(4)*x(9)-2*x(4)*x(10)-16*x(4)*x(11)-30*x(4)*x(12)+53*x(5)*x(5)+124*x(5)*x(6)-18*x(5)*x(7)-36*x(5)*x(8)-54*x(5)*x(9)-4*x(5)*x(10)-22*x(5)*x(11)-40*x(5)*x(12)+73*x(6)*x(6)-22*x(6)*x(7)-44*x(6)*x(8)-66*x(6)*x(9)-6*x(6)*x(10)-28*x(6)*x(11)-50*x(6)*x(12)+2*x(7)*x(7)+8*x(7)*x(8)+12*x(7)*x(9)+2*x(7)*x(10)+6*x(7)*x(11)+10*x(7)*x(12)+8*x(8)*x(8)+24*x(8)*x(9)+4*x(8)*x(10)+12*x(8)*x(11)+20*x(8)*x(12)+18*x(9)*x(9)+6*x(9)*x(10)+18*x(9)*x(11)+30*x(9)*x(12)+1*x(10)*x(10)+4*x(10)*x(11)+6*x(10)*x(12)+5*x(11)*x(11)+16*x(11)*x(12)+26*x(12)*x(12));';

%The constrained function, one equality, function (15) and twelve "<=0"
fung = 'g = [x(1)+x(2)+x(3)+x(4)+x(5)+x(6)-x(7)-x(8)-x(9)-x(10)-x(11)-x(12); -x(1); -x(2); -x(3); -x(4); -x(5); -x(6); -x(7); -x(8); -x(9); -x(10); -x(11); -x(12)];';

%Indicate there one equality constrained in "fung"
options(13) = 1;

fun = [funf fung];

%Start point should be consistent with the number of variables 
R0 = [-0.5;0.6;-0.9;1;1;1;-0.5;0.6;-0.9;1;1;1];

% R=[R(1),R(2),...] is the optimization results of [x(1),x(2),...]
[R, options,L] = constr(fun, R0, options);

% Note!!!!, the Comprehension to "Lagrange Multiplier" x(i) or ai and the return parameters L
% L=[L(2),...,L(m+1)] is the Lagrange Multipliers for function f, the function (16);
% L(i)=0 indicate the sample corresponding is a "Support Vector" 
% or the constrained "x(i)>=0" is a active constrained
% Note!!! x(i) or, in function (16), the ai is also "Lagrange Multiplier" but it is for object function ||W||
% and here, the actual object function to be optimized is function f, the function (16);
% and the function (16) can be considered as an alternation of original object function ||W||
% and x(i)or,in function (16),the ai is only new variables of an alternation of origin variables.

% Calculate W, as function (14), W=sum(a1*y1*xi) 
R=diag(R);
Y=diag([ones(6,1);-ones(6,1)]);
W=sum((R*Y)*X);

figure(1);
whitebg(1,[0,0.1,0]);

plot([A(1:6,1:1)],[A(1:6,2:2)],'m*');
hold on
plot([B(1:6,1:1)],[B(1:6,2:2)],'c*');

% Any Support Vector, the sample with "Lagrange Multiplier L=0", satisfies "Yi*(W*X + b)=1" --function (12),
% then  substitute any sample X0 in the mind into (12), one can get " b = 1/Yi - W*X0 = Yi - W*X0 ";
% The classfication function "W*X + b = 0" can be determined as: y = -x*W(1)/W(2) - Yi/W(2) + W*X0/W(2)
X0=A(1:1,1:2)';
Xa=0;
Xb=4;
Ya=-1/W(2)+W*X0/W(2);
Yb=-Xb*W(1)/W(2)-1/W(2)+W*X0/W(2);
plot([0,4],[Ya,Yb],'y')

axis([0,4,min(-1,Ya),9])

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -