资源描述:
《Matlab的SVM算法进行线性和非线性分类实例_20131128》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Matlab_svmtranin_example1.Linearclassification%TwoDimensionLinear-SVMProblem,TwoClassandSeparableSituation%MethodfromChristopherJ.C.Burges:%"ATutorialonSupportVectorMachinesforPatternRecognition",page9%Optimizing
2、
3、W
4、
5、directly:%Objective:min"f(A)=
6、
7、W
8、
9、",p8/li
10、ne26%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
11、(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%MethodfromChristophe
12、rJ.C.Burges:%"ATutorialonSupportVectorMachinesforPatternRecognition",page9%Optimizing
13、
14、W
15、
16、directly:%Objective:min"f(A)=
17、
18、W
19、
20、",p8/line26%Subjectto:yi*(xi*W+b)-1>=0,function(12);clearall;6closeallclc;sp=[3,7;6,6;4,6;5,6.5]%positivesamplepointsnsp=size(sp);sn=[
21、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)
22、%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)%RDistheclassificationresultv
23、ector1.SvmtrainsvmtrainTrainasupportvectormachineclassifierSVMSTRUCT=svmtrain(TRAINING,Y)trainsasupportvectormachine(SVM)classifierondatatakenfromtwogroups.TRAININGisanumericmatrixofpredictordata.RowsofTRAININGcorrespondtoobservations;columnscorrespondtofeat
24、ures.Yisacolumnvectorthatcontainstheknown6classlabelsforTRAINING.Yisagroupingvariable,i.e.,itcanbeacategorical,numeric,orlogicalvector;acellvectorofstrings;oracharactermatrixwitheachrowrepresent