您好,欢迎来到爱玩科技网。
搜索
您的当前位置:首页Matlab的SVM算法进行线性和非线性分类实例_20131128

Matlab的SVM算法进行线性和非线性分类实例_20131128

来源:爱玩科技网
Matlab_svmtranin_example

1.Linearclassification%TwoDimensionLinear-SVMProblem,TwoClassandSeparableSituation%MethodfromChristopherJ.C.Burges:%\"ATutorialonSupportVectorMachinesforPatternRecognition\page9%Optimizing||W||directly:%Objective:min\"f(A)=||W||\",p8/line26%Subjectto:yi*(xi*W+b)-1>=0,function(12);clearall;closeallclc;sp=[3,7;6,6;4,6;5,6.5]%positivesamplepointsnsp=size(sp);sn=[1,2;3,5;7,3;3,4;6,2.7]%negativesamplepointsnsn=size(sn)sd=[sp;sn]lsd=[truetruetruetruefalsefalsefalsefalsefalse]Y=nominal(lsd)figure(1);subplot(1,2,1)plot(sp(1:nsp,1),sp(1:nsp,2),'m+');holdonplot(sn(1:nsn,1),sn(1:nsn,2),'c*');subplot(1,2,2)svmStruct=svmtrain(sd,Y,'showplot',true);2.NonLinearclassification%TwoDimensionquadratic-SVMProblem,TwoClassandSeparableSituation%MethodfromChristopherJ.C.Burges:%\"ATutorialonSupportVectorMachinesforPatternRecognition\page9%Optimizing||W||directly:%Objective:min\"f(A)=||W||\",p8/line26%Subjectto:yi*(xi*W+b)-1>=0,function(12);clearall;1closeallclc;sp=[3,7;6,6;4,6;5,6.5]%positivesamplepointsnsp=size(sp);sn=[1,2;3,5;7,3;3,4;6,2.7;4,3;2,7]%negativesamplepointsnsn=size(sn)sd=[sp;sn]lsd=[truetruetruetruefalsefalsefalsefalsefalsefalsefalse]Y=nominal(lsd)figure(1);subplot(1,2,1)plot(sp(1:nsp,1),sp(1:nsp,2),'m+');holdonplot(sn(1:nsn,1),sn(1:nsn,2),'c*');subplot(1,2,2)%svmStruct=svmtrain(sd,Y,'Kernel_Function','linear','showplot',true);svmStruct=svmtrain(sd,Y,'Kernel_Function','quadratic','showplot',true);%usethetrainedsvm(svmStruct)toclassifythedataRD=svmclassify(svmStruct,sd,'showplot',true)%RDistheclassificationresultvector3.SvmtrainsvmtrainTrainasupportvectormachineclassifierSVMSTRUCT=svmtrain(TRAINING,Y)trainsasupportvectormachine(SVM)classifierondatatakenfromtwogroups.TRAININGisanumericmatrixofpredictordata.RowsofTRAININGcorrespondtoobservations;columnscorrespondtofeatures.Yisacolumnvectorthatcontainstheknown2aclasslabelsforTRAINING.Yisagroupingvariable,i.e.,itcanbeacategorical,numeric,orlogicalvector;acellvectorofstrings;orcharactermatrixwitheachrowrepresentingaclasslabel(seehelpforgroupingvariable).EachelementofYspecifiesthegroupthecorrespondingrowofTRAININGbelongsto.TRAININGandYmusthavethesamenumberofrows.SVMSTRUCTcontainsinformationaboutthetrainedclassifier,includingthesupportvectors,thatisusedbySVMCLASSIFYforclassification.svmtraintreatsNaNs,emptystringsor'undefined'valuesasmissingvaluesandignoresthecorrespondingrowsinTRAININGandY.SVMSTRUCT=svmtrain(TRAINING,Y,'PARAM1',val1,'PARAM2',val2,...)specifiesoneormoreofthefollowingname/valuepairs:NameValue'kernel_function'Astringorafunctionhandlespecifyingthekernelfunctionusedtorepresentthedotproductinanewspace.Thevaluecanbeoneofthefollowing:'linear'-Linearkernelordotproduct(default).Inthiscase,svmtrainfindstheoptimalseparatingplaneintheoriginalspace.'quadratic'-Quadratickernel'polynomial'-Polynomialkernelwithdefaultorder3.Tospecifyanotherorder,usethe'polyorder'argument.'rbf'-GaussianRadialBasisFunctionwithdefaultscalingfactor1.Tospecifyanotherscalingfactor,usethe'rbf_sigma'argument.'mlp'-MultilayerPerceptronkernel(MLP)withdefaultweight1anddefaultbias-1.Tospecifyanotherweightorbias,usethe'mlp_params'argument.function-Akernelfunctionspecifiedusing@(forexample@KFUN),orananonymousfunction.AkernelfunctionmustbeoftheformfunctionK=KFUN(U,V)Thereturnedvalue,K,isamatrixofsizeM-by-N,whereMandNarethenumberofrowsinUandVrespectively.'rbf_sigma'ApositivenumberspecifyingthescalingfactorintheGaussianradialbasisfunctionkernel.Defaultis1.Apositiveintegerspecifyingtheorderofthepolynomialkernel.Defaultis3.Avector[P1P2]specifyingtheparametersofMLPkernel.TheMLPkerneltakestheform:3'polyorder''mlp_params'K=tanh(P1*U*V'+P2),whereP1>0andP2<0.Defaultis[1,-1].'method'Astringspecifyingthemethodusedtofindtheseparatinghyperplane.Choicesare:'SMO'-SequentialMinimalOptimization(SMO)method(default).ItimplementstheL1soft-marginSVMclassifier.'QP'-Quadraticprogramming(requiresanOptimizationToolboxlicense).ItimplementstheL2soft-marginSVMclassifier.Method'QP'doesn'tscalewellforTRAININGwithlargenumberofobservations.'LS'-Least-squaresmethod.ItimplementstheL2soft-marginSVMclassifier.OptionsstructurecreatedusingeitherSTATSETorOPTIMSET.*Whenyouset'method'to'SMO'(default),createtheoptionsstructureusingSTATSET.Applicableoptions:'Display'Levelofdisplayoutput.Choicesare'off'(thedefault),'iter',and'final'.Value'iter'reportsevery500iterations.'MaxIter'Apositiveintegerspecifyingthemaximumnumberofiterationsallowed.Defaultis15000formethod'SMO'.*Whenyousetmethodto'QP',createtheoptionsstructureusingOPTIMSET.Fordetailsofapplicableoptionschoices,seeQUADPROGoptions.SVMusesaconvexquadraticprogram,soyoucanchoosethe'interior-point-convex'algorithminQUADPROG.ApositivescalarthatspecifiesthetolerancewithwhichtheKarush-Kuhn-Tucker(KKT)conditionsarecheckedformethod'SMO'.Defaultis1.0000e-003.'options''tolkkt''kktviolationlevel'AscalarspecifyingthefractionofobservationsthatareallowedtoviolatetheKKTconditionsformethod'SMO'.Settingthisvaluetobepositivehelpsthealgorithmtoconvergefasterifitisfluctuatingnearagoodsolution.Defaultis0.'kernelcachelimit'ApositivescalarSspecifyingthesizeofthekernelmatrixcacheformethod'SMO'.ThealgorithmkeepsamatrixwithuptoS*Sdouble-precisionnumbersinmemory.Defaultis5000.WhenthenumberofpointsinTRAININGexceedsS,theSMOmethodslowsdown.It'srecommendedtosetSaslargeasyoursystempermits.TheboxconstraintCforthesoftmargin.Ccanbe4'boxconstraint'apositivenumericscalaroravectorofpositivenumberswiththenumberofelementsequaltothenumberofrowsinTRAINING.Defaultis1.*IfCisascalar,itisautomaticallyrescaledbyN/(2*N1)fortheobservationsofgroupone,andbyN/(2*N2)fortheobservationsofgrouptwo,whereN1isthenumberofobservationsingroupone,N2isthenumberofobservationsingrouptwo.Therescalingisdonetotakeintoaccountunbalancedgroups,i.e.,whenN1andN2aredifferent.*IfCisavector,theneachelementofCspecifiestheboxconstraintforthecorrespondingobservation.'autoscale'Alogicalvaluespecifyingwhetherornottoshiftandscalethedatapointsbeforetraining.Whenthevalueistrue,thecolumnsofTRAININGareshiftedandscaledtohavezeromeanunitvariance.Defaultistrue.Alogicalvaluespecifyingwhetherornottoshowaplot.Whenthevalueistrue,svmtraincreatesaplotofthegroupeddataandtheseparatinglinefortheclassifier,whenusingdatawith2features(columns).Defaultisfalse.'showplot'SVMSTRUCTisastructurehavingthefollowingproperties:SupportVectorsMatrixofdatapointswitheachrowcorrespondingtoasupportvector.Note:when'autoscale'isfalse,thisfieldcontainsoriginalsupportvectorsinTRAINING.When'autoscale'istrue,thisfieldcontainsshiftedandscaledvectorsfromTRAINING.AlphaVectorofLagrangemultipliersforthesupportvectors.Thesignispositiveforsupportvectorsbelongingtothefirstgroupandnegativeforsupportvectorsbelongingtothesecondgroup.BiasInterceptofthehyperplanethatseparatesthetwogroups.Note:when'autoscale'isfalse,thisfieldcorrespondstotheoriginaldatapointsinTRAINING.When'autoscale'istrue,thisfieldcorrespondstoshiftedandscaleddatapoints.KernelFunctionThefunctionhandleofkernelfunctionused.KernelFunctionArgsCellarraycontainingtheadditionalargumentsforthekernelfunction.GroupNamesAcolumnvectorthatcontainstheknownclasslabelsforTRAINING.Yisagroupingvariable(seehelpforgroupingvariable).SupportVectorIndicesAcolumnvectorindicatingtheindicesofsupportvectors.ScaleDataThisfieldcontainsinformationaboutauto-scale.When'autoscale'isfalse,itisempty.When'autoscale'issettotrue,itisastructure5FigureHandlescontainingtwofields:shift-ArowvectorcontainingthenegativeofthemeanacrossallobservationsinTRAINING.scaleFactor-Arowvectorwhosevalueis1./STD(TRAINING).Avectoroffigurehandlescreatedbysvmtrainwhen'showplot'argumentisTRUE.Example:%LoadthedataandselectfeaturesforclassificationloadfisheririsX=[meas(:,1),meas(:,2)];%ExtracttheSetosaclassY=nominal(ismember(species,'setosa'));%Randomlypartitionsobservationsintoatrainingsetandatest%setusingstratifiedholdoutP=cvpartition(Y,'Holdout',0.20);%UsealinearsupportvectormachineclassifiersvmStruct=svmtrain(X(P.training,:),Y(P.training),'showplot',true);C=svmclassify(svmStruct,X(P.test,:),'showplot',true);errRate=sum(Y(P.test)~=C)/P.TestSize%mis-classificationrateconMat=confusionmat(Y(P.test),C)%theconfusionmatrix6

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- aiwanbo.com 版权所有 赣ICP备2024042808号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务