支持向量机的matlab代码.docx
- 文档编号:9963405
- 上传时间:2023-02-07
- 格式:DOCX
- 页数:15
- 大小:18.65KB
支持向量机的matlab代码.docx
《支持向量机的matlab代码.docx》由会员分享,可在线阅读,更多相关《支持向量机的matlab代码.docx(15页珍藏版)》请在冰豆网上搜索。
支持向量机的matlab代码
支持向量机的matlab代码
如果是7.0以上版本
>>editsvmtrain
>>editsvmclassify
>>editsvmpredict
function[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
%>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.
%setdefaults
plotflag=false;
qp_opts=[];
kfunargs={};
setPoly=false;usePoly=false;
setMLP=false;useMLP=false;
if~isempty(which('quadprog'))
useQuadprog=true;
else
useQuadprog=false;
end
%setdefaultkernelfunction
kfun=@linear_kernel;
%checkinputs
ifnargin<2
error(nargchk(2,Inf,nargin))
end
numoptargs=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)==1
groupnames=groupnames';
end
%makesuredataistherightsize
n=length(groupnames);
ifsize(training,1)~=n
ifsize(training,2)==n
training=training';
else
error('Bioinfo:
svmtrain:
DataGroupSizeMismatch',...
'GROUPandTRAININGmusthavethesamenumberofrows.')
end
end
%NaNsaretreatedasunknownclassesandareremovedfromthetraining
%data
nans=find(isnan(g));
iflength(nans)>0
training(nans,:
)=[];
g(nans)=[];
end
ngroups=length(groupString);
ifngroups>2
error('Bioinfo:
svmtrain:
TooManyGroups',...
'SVMTRAINonlysupportsclassificationintotwogroups.\nGROUPcontains%ddifferentgroups.',ngroups)
end
%convertto1,-1.
g=1-(2*(g-1));
%handleoptionalarguments
ifnumoptargs>=1
ifrem(numoptargs,2)==1
error('Bioinfo:
svmtrain:
IncorrectNumberOfArguments',...
'Incorrectnumberofargumentsto%s.',mfilename);
end
okargs={'kernel_function','method','showplot','kfunargs','quadprog_opts','polyorder','mlp_params'};
forj=1:
2:
numoptargs
pname=optargs{j};
pval=optargs{j+1};
k=strmatch(lower(pname),okargs);%#ok
ifisempty(k)
error('Bioinfo:
svmtrain:
UnknownParameterName',...
'Unknownparametername:
%s.',pname);
elseiflength(k)>1
error('Bioinfo:
svmtrain:
AmbiguousParameterName',...
'Ambiguousparametername:
%s.',pname);
else
switch(k)
case1%kernel_function
ifischar(pval)
okfuns={'linear','quadratic',...
'radial','rbf','polynomial','mlp'};
funNum=strmatch(lower(pval),okfuns);%#ok
ifisempty(funNum)
funNum=0;
end
switchfunNum%maybemakethislessstrictinthefuture
case1
kfun=@linear_kernel;
case2
kfun=@quadratic_kernel;
case{3,4}
kfun=@rbf_kernel;
case5
kfun=@poly_kernel;
usePoly=true;
case6
kfun=@mlp_kernel;
useMLP=true;
otherwise
error('Bioinfo:
svmtrain:
UnknownKernelFunction',...
'UnknownKernelFunction%s.',kfun);
end
elseifisa(pval,'function_handle')
kfun=pval;
else
error('Bioinfo:
svmtrain:
BadKernelFunction',...
'Thekernelfunctioninputdoesnotappeartobeafunctionhandle\norvalidfunctionname.')
end
case2%method
ifstrncmpi(pval,'qp',2)
useQuadprog=true;
ifisempty(which('quadprog'))
warning('Bioinfo:
svmtrain:
NoOptim',...
'TheOptimizationToolboxisrequiredtousethequadraticprogrammingmethod.')
useQuadprog=false;
end
elseifstrncmpi(pval,'ls',2)
useQuadprog=false;
else
error('Bioinfo:
svmtrain:
UnknownMethod',...
'Unknownmethodoption%s.Validmethodsare''QP''and''LS''',pval);
end
case3%display
ifpval~=0
ifsize(training,2)==2
plotflag=true;
else
warning('Bioinfo:
svmtrain:
OnlyPlot2D',...
'Thedisplayoptioncanonlyplot2Dtrainingdata.')
end
end
case4%kfunargs
ifiscell(pval)
kfunargs=pval;
else
kfunargs={pval};
end
case5%quadprog_opts
ifisstruct(pval)
qp_opts=pval;
elseifiscell(pval)
qp_opts=optimset(pval{:
});
else
error('Bioinfo:
svmtrain:
BadQuadprogOpts',...
'QUADPROG_OPTSmustbeanoptsstructure.');
end
case6%polyorder
if~isscalar(pval)||~isnumeric(pval)
error('Bioinfo:
svmtrain:
BadPolyOrder',...
'POLYORDERmustbeascalarvalue.');
end
ifpval~=floor(pval)||pval<1
error('Bioinfo:
svmtrain:
PolyOrderNotInt',...
'Theorderofthepolynomialkernelmustbeapositiveinteger.')
end
kfunargs={pval};
setPoly=true;
case7%mlpparams
ifnumel(pval)~=2
error('Bioinfo:
svmtrain:
BadMLPParams',...
'MLP_PARAMSmustbeatwoelementarray.');
end
if~isscalar(pval
(1))||~isscalar(pval
(2))
error('Bioinfo:
svmtrain:
MLPParamsNotScalar',...
'Theparametersofthemulti-layerperceptronkernelmustbescalar.');
end
kfunargs={pval
(1),pval
(2)};
setMLP=true;
end
end
end
end
ifsetPoly&&~usePoly
warning('Bioinfo:
svmtrain:
PolyOrderNotPolyKernel',...
'Youspecifiedapolynomialorderbutnotapolynomialkernel');
end
ifsetMLP&&~useMLP
warning('Bioinfo:
svmtrain:
MLPParamNotMLPKernel',...
'YouspecifiedMLPparametersbutnotanMLPkernel');
end
%plotthedataifrequested
ifplotflag
[hAxis,hLines]=svmplotdata(training,g);
legend(hLines,cellstr(groupString));
end
%calculatekernelfunction
try
kx=feval(kfun,training,training,kfunargs{:
});
%ensurefunctionissymmetric
kx=(kx+kx')/2;
catch
error('Bioinfo:
svmtrain:
UnknownKernelFunction',...
'Errorcalculatingthekernelfunction:
\n%s\n',lasterr);
end
%createHessian
%addsmallconstanteyetoforcestability
H=((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),in
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 支持 向量 matlab 代码
![提示](https://static.bdocx.com/images/bang_tan.gif)