editsvmtraineditsvmclassifyeditsvmpredictfunction[svm_struct,svIndex]=svmtrain(training,groupnames,varargin)%SVMTRAINtrainsasupportvectormachineclassifier%%SVMStruct=SVMTRAIN(TRAINING,GROUP)trainsasupportvectormachine%classifierusingdataTRAININGtakenfromtwogroupsgivenbyGROUP.%SVMStructcontainsinformationaboutthetrainedclassifierthatis%usedbySVMCLASSIFYforclassification.GROUPisacolumnvectorof%valuesofthesamelengthasTRAININGthatdefinestwogroups.Each%elementofGROUPspecifiesthegroupthecorrespondingrowofTRAINING%belongsto.GROUPcanbeanumericvector,astringarray,oracell%arrayofstrings.SVMTRAINtreatsNaNsoremptystringsinGROUPas%missingvaluesandignoresthecorrespondingrowsofTRAINING.%%SVMTRAIN(...,'KERNEL_FUNCTION',KFUN)allowsyoutospecifythekernel%functionKFUNusedtomapthetrainingdataintokernelspace.The%defaultkernelfunctionisthedotproduct.KFUNcanbeoneofthe%followingstringsorafunctionhandle:%%'linear'Linearkernelordotproduct%'quadratic'Quadratickernel%'polynomial'Polynomialkernel(defaultorder3)%'rbf'GaussianRadialBasisFunctionkernel%'mlp'MultilayerPerceptronkernel(defaultscale1)%functionAkernelfunctionspecifiedusing@,%forexample@KFUN,orananonymousfunction%%Akernelfunctionmustbeoftheform%%functionK=KFUN(U,V)%%Thereturnedvalue,K,isamatrixofsizeM-by-N,whereUandVhaveM%andNrowsrespectively.IfKFUNisparameterized,youcanuse%anonymousfunctionstocapturetheproblem-dependentparameters.For%example,supposethatyourkernelfunctionis%%functionk=kfun(u,v,p1,p2)%k=tanh(p1*(u*v')+p2);%%Youcansetvaluesforp1andp2andthenuseananonymousfunction:%@(u,v)kfun(u,v,p1,p2).%%SVMTRAIN(...,'POLYORDER',ORDER)allowsyoutospecifytheorderofa%polynomialkernel.Thedefaultorderis3.%%SVMTRAIN(...,'MLP_PARAMS',[P1P2])allowsyoutospecifythe%parametersoftheMultilayerPerceptron(mlp)kernel.Themlpkernel%requirestwoparameters,P1andP2,whereK=tanh(P1*U*V'+P2)andP1%0andP20.DefaultvaluesareP1=1andP2=-1.%%SVMTRAIN(...,'METHOD',METHOD)allowsyoutospecifythemethodused%tofindtheseparatinghyperplane.Optionsare%%'QP'Usequadraticprogramming(requirestheOptimizationToolbox)%'LS'Useleast-squaresmethod%%IfyouhavetheOptimizationToolbox,thentheQPmethodisthedefault%method.Ifnot,theonlyavailablemethodisLS.%%SVMTRAIN(...,'QUADPROG_OPTS',OPTIONS)allowsyoutopassanOPTIONS%structurecreatedusingOPTIMSETtotheQUADPROGfunctionwhenusing%the'QP'method.Seehelpoptimsetformoredetails.%%SVMTRAIN(...,'SHOWPLOT',true),whenusedwithtwo-dimensionaldata,%createsaplotofthegroupeddataandplotstheseparatinglinefor%theclassifier.%%Example:%%Loadthedataandselectfeaturesforclassification%loadfisheriris%data=[meas(:,1),meas(:,2)];%%ExtracttheSetosaclass%groups=ismember(species,'setosa');%%Randomlyselecttrainingandtestsets%[train,test]=crossvalind('holdOut',groups);%cp=classperf(groups);%%Usealinearsupportvectormachineclassifier%svmStruct=svmtrain(data(train,:),groups(train),'showplot',true);%classes=svmclassify(svmStruct,data(test,:),'showplot',true);%%Seehowwelltheclassifierperformed%classperf(cp,classes,test);%cp.CorrectRate%%SeealsoCLASSIFY,KNNCLASSIFY,QUADPROG,SVMCLASSIFY.%Copyright2004TheMathWorks,Inc.%$Revision:1.1.12.1$$Date:2004/12/2420:43:35$%References:%[1]Kecman,V,LearningandSoftComputing,%MITPress,Cambridge,MA.2001.%[2]Suykens,J.A.K.,VanGestel,T.,DeBrabanter,J.,DeMoor,B.,%Vandewalle,J.,LeastSquaresSupportVectorMachines,%WorldScientific,Singapore,2002.%[3]Scholkopf,B.,Smola,A.J.,LearningwithKernels,%MITPress,Cambridge,MA.2002.%%SVMTRAIN(...,'KFUNARGS',ARGS)allowsyoutopassadditional%argumentstokernelfunctions.%setdefaultsplotflag=false;qp_opts=[];kfunargs={};setPoly=false;usePoly=false;setMLP=false;useMLP=false;if~isempty(which('quadprog'))useQuadprog=true;elseuseQuadprog=false;end%setdefaultkernelfunctionkfun=@linear_kernel;%checkinputsifnargin2error(nargchk(2,Inf,nargin))endnumoptargs=nargin-2;optargs=varargin;%grp2idxsortsanumericgroupingvarascending,andastringgrouping%varbyorderoffirstoccurrence[g,groupString]=grp2idx(groupnames);%checkgroupisavector--thoughcharinputisspecial...if~isvector(groupnames)&&~ischar(groupnames)error('Bioinfo:svmtrain:GroupNotVector',...'Groupmustbeavector.');end%makesurethatthedataiscorrectlyoriented.ifsize(groupnames,1)==1groupnames=groupnames';end%makesuredataistherightsizen=length(groupnames);ifsize(training,1)~=nifsize(training,2)==ntraining=training';elseerror('Bioinfo:svmtrain:DataGroupSizeMismatch',...'GROUPandTRAININGmusthavethesamenumberofrows.')endend%NaNsaretreatedasunknownclassesandareremovedfromthetraining%datanans=find(isnan(g));iflength(nans)0training(nans,:)=[];g(nans)=[];endngroups=length(groupString);ifngroups2error('Bioinfo:svmtrain:TooManyGroups',...'SVMTRAINonlysupportsclassificationintotwogroups.\nGROUPcontains%ddifferentgroups.',ngroups)end%convertto1,-1.g=1-(2*(g-1));%handleoptionalargumentsifnumoptargs=1ifrem(numoptargs,2)==1error('Bioinfo:svmtrain:IncorrectNumberOfArguments',...'Incorrectnumberofargumentsto%s.',mfilename);endokargs={'kernel_function','method','showplot','kfunargs','quadprog_opts','polyorder','mlp_params'};forj=1:2:numoptargsp