《支持向量机matlab实现源代码》由会员分享,可在线阅读,更多相关《支持向量机matlab实现源代码(8页珍藏版)》请在金锄头文库上搜索。
1、editsvmtraineditsvmclassifyeditsvmpredictfunctionsvm_struct,svIndex=svmtrain(training,groupnames,varargin)%SVMTRAINtrainsasupportvectormachineclassifier%SVMStruct=SVMTRAIN(TRAINING,GROUP)trainsasupportvectormachine%classifierusingdataTRAININGtakenfromtwogroupsgivenbyGROUP.%SVMStructcontainsinformati
2、onaboutthetrainedclassifierthatis%usedbySVMCLASSIFYforclassification.GROUPisacolumnvectorof%valuesofthesamelengthasTRAININGthatdefinestwogroups.Each%elementofGROUPspecifiesthegroupthecorrespondingrowofTRAINING%belongsto.GROUPcanbeanumericvector,astringarray,oracell%arrayofstrings.SVMTRAINtreatsNaNso
3、remptystringsinGROUPas%missingvaluesandignoresthecorrespondingrowsofTRAINING.%SVMTRAIN(.,KERNEL_FUNCTION,KFUN)allowsyoutospecifythekernel%functionKFUNusedtomapthetrainingdataintokernelspace.The%defaultkernelfunctionisthedotproduct.KFUNcanbeoneofthe%followingstringsorafunctionhandle:%linearLinearkern
4、elordotproduct%quadraticQuadratickernel%polynomialPolynomialkernel(defaultorder3)%rbfGaussianRadialBasisFunctionkernel%mlpMultilayerPerceptronkernel(defaultscale1)%functionAkernelfunctionspecifiedusing,%forexampleKFUN,orananonymousfunction%Akernelfunctionmustbeoftheform%functionK=KFUN(U,V)%Thereturn
5、edvalue,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);%Youcansetvaluesforp1andp2andthenuseananonymousfunc
6、tion:%(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.Def
7、aultvaluesareP1=1andP2=-1.%SVMTRAIN(.,METHOD,METHOD)allowsyoutospecifythemethodused%tofindtheseparatinghyperplane.Optionsare%QPUsequadraticprogramming(requirestheOptimizationToolbox)%LSUseleast-squaresmethod%IfyouhavetheOptimizationToolbox,thentheQPmethodisthedefault%method.Ifnot,theonlyavailablemet
8、hodisLS.%SVMTRAIN(.,QUADPROG_OPTS,OPTIONS)allowsyoutopassanOPTIONS%structurecreatedusingOPTIMSETtotheQUADPROGfunctionwhenusing%theQPmethod.Seehelpoptimsetformoredetails.%SVMTRAIN(.,SHOWPLOT,true),whenusedwithtwo-dimensionaldata,%createsaplotofthegroupeddataandplotstheseparatinglinefor%theclassifier.
9、%Example:%Loadthedataandselectfeaturesforclassification%loadfisheriris%data=meas(:,1),meas(:,2);%ExtracttheSetosaclass%groups=ismember(species,setosa);%Randomlyselecttrainingandtestsets%train,test=crossvalind(holdOut,groups);%cp=classperf(groups);%Usealinearsupportvectormachineclassifier%svmStruct=s
10、vmtrain(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/
11、2420:43:35$%References:%1Kecman,V,LearningandSoftComputing,%MITPress,Cambridge,MA.2001.%2Suykens,J.A.K.,VanGestel,T.,DeBrabanter,J.,DeMoor,B.,%Vandewalle,J.,LeastSquaresSupportVectorMachines,%WorldScientific,Singapore,2002.%3Scholkopf,B.,Smola,A.J.,LearningwithKernels,%MITPress,Cambridge,MA.2002.%SV
12、MTRAIN(.,KFUNARGS,ARGS)allowsyoutopassadditional%argumentstokernelfunctions.%setdefaultsplotflag=false;qp_opts=;kfunargs=;setPoly=false;usePoly=false;setMLP=false;useMLP=false;ifisempty(which(quadprog)useQuadprog=true;elseuseQuadprog=false;end%setdefaultkernelfunctionkfun=linear_kernel;%checkinputsifnargin0training(nans,:)=;g(nans)=;endngroups=length(groupString);ifngroups2error(Bioinfo:svmtrain:TooManyGroups,.SVMTRAINonlysupportsclassificationintotwogroups.nGROUPcontains%ddifferentgroups.,ngroups)end%convertto1,-1.g=1-(2*(g-1);%handleopt