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

📄 svmlspex01.m

📁 数据挖掘的新方法-支持向量机书中算法例子
💻 M
字号:
%SVMLSPex01.m
%
%Two Dimension Linear-SVM Problem, Two Class and Separable Situation
%
%Method from Christopher J. C. Burges:
%"A Tutorial on Support Vector Machines for Pattern Recognition", page 9
%
%Optimizing ||W|| directly:
%  Objective:   min "f(A)=||W||" ,		p8/line26
%  Subject to:  yi*(xi*W+b)-1>=0,		function (12);
%					  
%

function mm=SVMLSPex01(mm); %mm -- select sample number


echo off;
close all;
fclose all;
%Generate Two Set of Data each set with n data
	m1 = mm;					%Number of sample set A
   m2 = mm;					%Number of sample set B
   n = 2;					%Dimension of problem
  	al=pi/4;				%
	d=0.05;				%
   
	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(m1,2);
	x(1:m1,2)=2*x(1:m1,2);
	x(1:m1,1)=x(1:m1,1)+d;
	X2(1:m1,1)=x(1:m1,1)*cos(al)+x(1:m1,2)*sin(al);
	X2(1:m1,2)=x(1:m1,2)*cos(al)-x(1:m1,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
   	%
 		%Note!!! program below can processes any dimension sample set with (m*n)matrix, m-sample number/n-sample dimension.
		%
		%Transmiting Samples to Object and Constraint Function, in SVMSLPex01FUN.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);
      
      %Set start point to opmizing procedure
		x0 = [-1 1 1];															%start point for W(1),W(2) and b --- function (12)
      options = [];															%refrence to MATLAB function "FOPTIONS"
      [W, options,L] = constr('SVMLSPex01FUN', x0, options);	%Refrence to MATLAB function "constr"
      
      delete('a.dat');
      
		%Classifing function should be: y=kx+b, k=W(1)/W(2), b=W(3)/W(2)
      k=W(1)/W(2);
      b=W(3)/W(2);
		plot([Xa,Xb],[-Xa*k-b,-Xb*k-b]);
   
   	%When equality is hold in function (12),the sample corresponded is Support Vector
   		for j=1:m1+m2, 
		      if j<=m1
 		        yj=1;
 		     	else
 		        yj=-1;
            end
            er=yj*(X(j,1:2)*W(1:2)'+W(3))-1;		%Function (12)
		      if er<=1e-6,
		         plot(X(j,1),X(j,2),'wo');
		      end
		   end

end

⌨️ 快捷键说明

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