
支持向量机matlab实现源代码.doc
9页edit svmtrain >>edit svmclassify >>edit svmpredict function [svm_struct, svIndex] = svmtrain(training, groupnames, varargin) %SVMTRAIN trains a support vector machine classifier % % SVMStruct = SVMTRAIN(TRAINING,GROUP) trains a support vector machine % classifier using data TRAINING taken from two groups given by GROUP. % SVMStruct contains information about the trained classifier that is % used by SVMCLASSIFY for classification. GROUP is a column vector of % values of the same length as TRAINING that defines two groups. Each % element of GROUP specifies the group the corresponding row of TRAINING % belongs to. GROUP can be a numeric vector, a string array, or a cell % array of strings. SVMTRAIN treats NaNs or empty strings in GROUP as % missing values and ignores the corresponding rows of TRAINING. % % SVMTRAIN(...,'KERNEL_FUNCTION',KFUN) allows you to specify the kernel % function KFUN used to map the training data into kernel space. The % default kernel function is the dot product. KFUN can be one of the % following strings or a function handle: % % 'linear' Linear kernel or dot product % 'quadratic' Quadratic kernel % 'polynomial' Polynomial kernel (default order 3) % 'rbf' Gaussian Radial Basis Function kernel % 'mlp' Multilayer Perceptron kernel (default scale 1) % function A kernel function specified using @, % for example @KFUN, or an anonymous function % % A kernel function must be of the form % % function K = KFUN(U, V) % % The returned value, K, is a matrix of size M-by-N, where U and V have M % and N rows respectively. If KFUN is parameterized, you can use % anonymous functions to capture the problem-dependent parameters. For % example, suppose that your kernel function is % % function k = kfun(u,v,p1,p2) % k = tanh(p1*(u*v')+p2); % % You can set values for p1 and p2 and then use an anonymous function: % @(u,v) kfun(u,v,p1,p2). % % SVMTRAIN(...,'POLYORDER',ORDER) allows you to specify the order of a % polynomial kernel. The default order is 3. % % SVMTRAIN(...,'MLP_PARAMS',[P1 P2]) allows you to specify the % parameters of the Multilayer Perceptron (mlp) kernel. The mlp kernel % requires two parameters, P1 and P2, where K = tanh(P1*U*V' + P2) and P1 % > 0 and P2 0 training(nans,:) = []; g(nans) = []; end ngroups = length(groupString); if ngroups > 2 error('Bioinfo:svmtrain:TooManyGroups',... 'SVMTRAIN only supports classification into two groups.\nGROUP contains %d different groups.',ngroups) end % convert to 1, -1. g = 1 - (2* (g-1)); % handle optional arguments if numoptargs >= 1 if rem(numoptargs,2)== 1 error('Bioinfo:svmtrain:IncorrectNumberOfArguments',... 'Incorrect number of arguments to %s.',mfilename); end okargs = {'kernel_function','method','showplot','kfunargs','quadprog_opts','polyorder','mlp_params'}; for j=1:2:numoptargs pname = optargs{j}; pval = optargs{j+1}; k = strmatch(lower(pname), okargs);%#ok if isempty(k) error('Bioinfo:svmtrain:UnknownParameterName',... 'Unknown parameter name: %s.',pname); elseif length(k)>1 error('Bioinfo:svmtrain:AmbiguousParameterName',... 'Ambiguous parameter name: %s.',pname); else switch(k) case 1 % kernel_function if ischar(pval) okfuns = {'linear','quadratic',... 'radial','rbf','polynomial','mlp'}; funNum = strmatch(lower(pval), okfuns);%#ok if isempty(funNum) funNum = 0; end switch funNum %maybe make this less strict in the future case 1 kfun = @linear_kernel; case 2 kfun = @quadratic_kernel; case {3,4} kfun = @rbf_kernel; case 5 kfun = @poly_kernel; usePoly = true; case 6 kfun = @mlp_kernel; useMLP = true; otherwise error('Bioinfo:svmtrain:UnknownKernelFunction',... 'Unknown Kernel Function %s.',kfun); end elseif isa (pval, 'function_handle') kfun = pval; else error('Bioinfo:svmtrain:BadKernelFunction',... 'The kernel function input does not appear to be a function handle\nor valid function name.') end case 2 % method if strncmpi(pval,'qp',2) useQuadprog = true; if isempty(which('quadprog')) warning('Bioinfo:svmtrain:NoOptim',... 'The Optimization Toolbox is required to use the quadratic programming method.') useQuadprog = false; end elseif strncmpi(pval,'ls',2) useQuadprog = false; else error('Bioinfo:svmtrain:UnknownMethod',... 'Unknown method option %s. Valid methods are ''QP'' and ''LS''',pval); end case 3 % display if pval ~= 0 if size(training,2) == 2 plotflag = true; else warning('Bioinfo:svmtrain:OnlyPlot2D',... 'The display option can only plot 2D training data.') end end case 4 % kfunargs if iscell(pval) kfunargs = pval; else kfunargs = {pval}; end case 5 % quadprog_opts if isstruct(pval) qp_opts = pval; elseif iscell(pval) qp_opts = optimset(pval{:}); else error('Bioinfo:svmtrain:BadQuadprogOpts',... 'QUADPROG_OPTS must be an opts structure.'); end case 6 % polyorder if ~isscalar(pval) || ~isnumeric(pval) error('Bioinfo:svmtrain:BadPolyOrder',... 'POLYORDER must be a scalar value.'); end if pval ~=floor(pval) || pval sqrt(eps)); sv = training(svIndex,:); % calculate the parameters of the separating line from the support % vectors.。












