svmlspex01fun.m

来自「数据挖掘的新方法-支持向量机书中算法例子」· M 代码 · 共 37 行

M
37
字号
%Object function and constrained function for SVMLSPex01.m:
%
%  Objective:   min "f(A)=||W||" ,		p8/line26
%  Subject to:  yi*(xi*W+b)-1>=0,		function (12);
%
%a.dat file contained two classes samples and both are equal in number.
%Positive samples corresponding to Yi = 1, are arranged first in the a.dat with nigative ones followed

function [F,G]= SVMLSPex01FUN(Q);

	[m,n]=size(Q);
	if n>m,
	   Q=Q';
	end
	W=Q(1:2);
	b=Q(3);
	clear Q;

%Read the samples
	fid=fopen('a.dat','r');
   m1=fread(fid,1,'float');		%Number of class A
   m2=fread(fid,1,'float');		%Number of class B
   n=fread(fid,1,'float');			%Dimension of problem
   m=m1+m2;								%Total number of the samples
   	for k=1:n
  		    X(1:m,k)=fread(fid,m,'float');
   	end
   fclose(fid);
   
%Samples with m*n matrix and each row represent a sample vector.

	F=W'*W;

	Y=[ones(m1,1);-ones(m2,1)];		%Classifing value for two classes
	G=1-diag(Y)*(X*W+b);					%Constraint is function (12), keep to MATLAB should be C(x)<=0

⌨️ 快捷键说明

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