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

📄 svmlspex03.m

📁 数据挖掘的新方法-支持向量机书中算法例子
💻 M
字号:
%SVMLSPex03.m
%(Discrib the Object Function With Matrix Style)
%Two Dimension SVM Problem, Two Class and Separable Situation
%
%Method from Thorsten Joachims:
%"Making Large-Scale SVM Learning Practical", function (4)/(5)/(6)
%	
%  Objective:   min "f(A)=-sum(A)+A'*Q*A/2" ,							function (4);
%  Subject to:  sum{A'*Y}=0 ,													function (5);
%					 0 <= ai <= C ,												function (6);
%The optimizing variables is "Lagrange Multipliers": A=[a1,a2,...,am],m is the number of total samples.
%

function mm=SVMLSex03(mm) %mm -- select sample number

echo off;
close all;
fclose all;

%Generate Two Set of Data each set with n data
	m1=mm;					% The number of class A
   m2=mm;					% The number of class B
   n = 2;					% The dimension of sample vector
   al=pi/4;				%
	d=0.01;				%
   
	x=rand(m1,2);
	x(1:m1,1)=2*x(1:m1,1);
	x(1:m1,2)=x(1:m1,2);
	X1(1:m1,1)=x(1:m1,1)*cos(al)-x(1:m1,2)*sin(al);
	X1(1:m1,2)=x(1:m1,2)*cos(al)+x(1:m1,1)*sin(al);

	x=rand(m2,2);
	x(1:m2,2)=2*x(1:m2,2);
	x(1:m2,1)=x(1:m2,1)+d;
	X2(1:m2,1)=x(1:m2,1)*cos(al)+x(1:m2,2)*sin(al);
	X2(1:m2,2)=x(1:m2,2)*cos(al)-x(1:m2,1)*sin(al);
   
   clear x;
   
   
   X=[X1;X2];							%X1 is Positive Samples Set and X2 is Negative Samples Set
   m=m1+m2;								%Total number of samples
     
   figure(1);
  	whitebg(1,[0,0.1,0]);
   
   plot(X(1:m1,1),X(1:m1,2),'m*');
   hold on
   plot(X(m1+1:m,1),X(m1+1:m,2),'c*');
   
   Xa = min(0,min(X(1:m,1)));
   Xb = max(X(1:m,1));
	axis([Xa,Xb,min(X(1:m,2)),max(X(1:m,2))])
   
   asw = 0;
   while (asw~=1)&(asw~=2)
      asw=input('Continue or no? (1/2)');
   end
   if asw==1
   	%
		%
		%Transmiting Samples to Object and Constraint Function, in SVMLSex02FUN.m,
		%Reference to MATLAB function "constr"
		fid=fopen('a.dat','w');
      fwrite(fid,[m1,m2,n],'float');
      for k=1:n
         fwrite(fid,X(1:m,k),'float');
      end
      fclose(fid);
		%
		%Keep to MATLAB prescribe,Here
		%x0 is start point to optimizing;
      %Set options(13)=1 indicate the first constrained function contained in SVMnLSPex01FUN.m is a equality,
      %and others are "<=0",Reference to MATLAB function "CONSTR". Of cause, here we not other constraint;
      %
   	x0 = 0.5*ones(m,1);	%Initial of op					
      options(13) = 1;
      
	   C = 500;					% The up-bound of Lagrange Multiplier
      VLB = zeros(m,1);
      VUB = C*ones(m,1);
%
% About The up-bound of Lagrange Multiplier:  
% A Multiplier a(i) isn't zero indicate a corresponding constranit in the original optimizing problem is active.
% That is the constraint yi*(W*xi +b )-1>=0 is active, on the other words, sample (xi,yi) is a SVs.
% The other Multiplier a(j<>i) corresponding to the rest samples who aren't SV.
%
% But, too small up-bound imposed to Multipliers will cause a Mutiplier who corrrespond un-active constraint 
% cann't descent to zeros in optimizing procedure. 
% However, we decide if a sample is SV just only dependent on the fact that its Multiplier is zero or no.
%
     
		[A,options,L] = constr('SVMLSPex03FUN', x0, options, VLB, VUB);
      %
		%Return parameter: 
		%  A=[A(i)] is the optimizing variables, Lagrange Multipliyer;

	   Y=[ones(m1,1);-ones(m2,1)];	%The classfication value for both Positive and Negative Samples Set
		W = sum((diag(A)*diag(Y))*X);

		[z,I]=max(A(1:m1));		%The Support Vectors are corresponding to a(i) <> 0;
		X0=X(I,1:n)';				%Pick out a arbitrary Support Vector to determine the "b" in classifying function
      
      %yi*(W*xi +b )-1>=0, when yi=1 then k = -W(1)/W(2) and b = yi/W(2)-W*X0/W(2);
      %The classifing function should be X*W + b = 0. It is y = k*x - b, here k and b are,
      k = -W(1)/W(2);
      b = Y(I)/W(2)-W*X0/W(2);
		plot([Xa,Xb],[k*Xa-b,k*Xb-b],'y')
   
   	for n=1:m
 	     if A(n) > 1e-6,
            plot(X(n,1),X(n,2),'wo');
 	     end
	   end
delete('a.dat');

end

⌨️ 快捷键说明

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