支持向量机matlab实现源代码_第1页
支持向量机matlab实现源代码_第2页
支持向量机matlab实现源代码_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

editsvmtrain>>editsvmclassify>>editsvmpredictfunction[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);%%Youcansetvaluesforplandp2andthenuseananonymousfunction:%@(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%>0andP2<0.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;%checkinputsifnargin<2error(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);ifngroups>2error('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:numoptargspname=optargs{j};pval=optargs{j+1};k=strmatch(lower(pname),okargs);%#okifisempty(k)error('Bioinfo:svmtrain:UnknownParameterName',...

'Unknownparametername:%s.',pname);elseiflength(k)>1error('Bioinfo:svmtrain:AmbiguousParameterName',...

'Ambiguousparametername:%s.',pname);elseswitch(k)case1%kernel_functionifischar(pval)okfuns={'linear','quadratic',...

'radial','rbf','polynomial','mlp'};funNum=strmatch(lower(pval),okfuns);%#okifisempty(funNum)funNum=0;endswitchfunNum%maybemakethislessstrictinthefuturecase1kfun=@linear_kernel;case2kfun=@quadratic_kernel;case{3,4}kfun=@rbf_kernel;case5kfun=@poly_kernel;usePoly=true;case6kfun=@mlp_kernel;useMLP=true;otherwiseerror('Bioinfo:svmtrain:UnknownKernelFunction',...

'UnknownKernelFunction%s.',kfun);endelseifisa(pval,'function_handle')kfun=pval;elseerror('Bioinfo:svmtrain:BadKernelFunction',...

'Thekernelfunctioninputdoesnotappeartobeafunctionhandle\norvalidfunctionname.')endcase2%methodifstrncmpi(pval,'qp',2)useQuadprog=true;ifisempty(which('quadprog'))warning('Bioinfo:svmtrain:NoOptim',...

'TheOptimizationToolboxisrequiredtousethequadraticprogrammingmethod.')useQuadprog=false;endelseifstrncmpi(pval,'ls',2)useQuadprog=false;elseerror('Bioinfo:svmtrain:UnknownMethod',...

'Unknownmethodoption%s.Validmethodsare''QP''and''LS''',pval);endcase3%displayifpval~=0ifsize(training,2)==2plotflag=true;elsewarning('Bioinfo:svmtrain:OnlyPlot2D',...

'Thedisplayoptioncanonlyplot2Dtrainingdata.')endendcase4%kfunargsifiscell(pval)kfunargs=pval;elsekfunargs={pval};endcase5%quadprog_optsifisstruct(pval)qp_opts=pval;elseifiscell(pval)qp_opts=optimset(pval{:});elseerror('Bioinfo:svmtrain:BadQuadprogOpts',...

'QUADPROG_OPTSmustbeanoptsstructure.');endcase6%polyorderif~isscalar(pval)||~isnumeric(pval)error('Bioinfo:svmtrain:BadPolyOrder',...

'POLYORDERmustbeascalarvalue.');endifpval~=floor(pval)||pval<1error('Bioinfo:svmtrain:PolyOrderNotInt',...

'Theorderofthepolynomialkernelmustbeapositiveinteger.')endkfunargs={pval};setPoly=true;case7%mlpparamsifnumel(pval)~=2error('Bioinfo:svmtrain:BadMLPParams',...

'MLP_PARAMSmustbeatwoelementarray.');endif~isscalar(pval(1))||~isscalar(pval(2))error('Bioinfo:svmtrain:MLPParamsNotScalar',...

'Theparametersofthemulti-layerperceptronkernelmustbescalar.');endkfunargs={pval(1),pval(2)};setMLP=true;endendendendifsetPoly&&~usePolywarning('Bioinfo:svmtrain:PolyOrderNotPolyKernel',...

'Youspecifiedapolynomialorderbutnotapolynomialkernel');endifsetMLP&&~useMLPwarning('Bioinfo:svmtrain:MLPParamNotMLPKernel',...

'YouspecifiedMLPparametersbutnotanMLPkernel');end%plotthedataifrequestedifplotflag[hAxis,hLines]=svmplotdata(training,g);legend(hLines,cellstr(groupString));end%calculatekernelfunctiontrykx=feval(kfun,training,training,kfunargs{:});%ensurefunctionissymmetrickx=(kx+kx')/2;catcherror('Bioinfo:svmtrain:UnknownKernelFunction',...

'Errorcalculatingthekernelfunction:\n%s\n',lasterr);end%createHessian%addsmallconstanteyetoforcestabilityH=((g*g').*kx)+sqrt(eps(class(training)))*eye(n);ifuseQuadprog%Thelargescalesolvercannothandlethistypeofproblem,soturnit%off.

qp_opts=optimset(qp_opts,'LargeScale','Off');%X=QUADPROG(H,f,A,b,Aeq,beq,LB,UB,X0,opts)alpha=quadprog(H,-ones(n,1),[],[],...

g',0,zeros(n,1),inf*ones(n,1),zeros(n,1),qp_opts);%Thesupportvectorsarethenon-zerosofalphasvIndex=find(alpha>sqrt(eps));sv=training(svIndex,:);%calculatetheparametersoftheseparatinglinefromthesupport%vectors.

alphaHat=g(svIndex).*alpha(svIndex);%Calculatethebiasbyapplyingtheindicatorfunctiontothesupport%vectorwithlargestalpha.

[maxAlpha,ma

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论