支持向量机matlab实现源代码Word格式文档下载.docx
《支持向量机matlab实现源代码Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《支持向量机matlab实现源代码Word格式文档下载.docx(12页珍藏版)》请在冰豆网上搜索。
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).
POLYORDER'
ORDER)allowsyoutospecifytheorderofa
%polynomialkernel.Thedefaultorderis3.
MLP_PARAMS'
[P1P2])allowsyoutospecifythe
%parametersoftheMultilayerPerceptron(mlp)kernel.Themlpkernel
%requirestwoparameters,P1andP2,whereK=tanh(P1*U*V'
+P2)andP1
%>
0andP2<
0.DefaultvaluesareP1=1andP2=-1.
METHOD'
METHOD)allowsyoutospecifythemethodused
%tofindtheseparatinghyperplane.Optionsare
QP'
Usequadraticprogramming(requirestheOptimizationToolbox)
LS'
Useleast-squaresmethod
%IfyouhavetheOptimizationToolbox,thentheQPmethodisthedefault
%method.Ifnot,theonlyavailablemethodisLS.
QUADPROG_OPTS'
OPTIONS)allowsyoutopassanOPTIONS
%structurecreatedusingOPTIMSETtotheQUADPROGfunctionwhenusing
%the'
method.Seehelpoptimsetformoredetails.
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,:
),'
%%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.
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))
numoptargs=nargin-2;
optargs=varargin;
%grp2idxsortsanumericgroupingvarascending,andastringgrouping
%varbyorderoffirstoccurrence
[g,groupString]=grp2idx(groupnames);
%checkgroupisavector--thoughcharinputisspecial...
if~isvector(groupnames)&
&
~ischar(groupnames)
error('
Bioinfo:
svmtrain:
GroupNotVector'
...
'
Groupmustbeavector.'
%makesurethatthedataiscorrectlyoriented.
ifsize(groupnames,1)==1
groupnames=groupnames'
;
%makesuredataistherightsize
n=length(groupnames);
ifsize(training,1)~=n
ifsize(training,2)==n
training=training'
DataGroupSizeMismatch'
GROUPandTRAININGmusthavethesamenumberofrows.'
)
%NaNsaretreatedasunknownclassesandareremovedfromthetraining
%data
nans=find(isnan(g));
iflength(nans)>
0
training(nans,:
)=[];
g(nans)=[];
ngroups=length(groupString);
ifngroups>
TooManyGroups'
SVMTRAINonlysupportsclassificationintotwogroups.\nGROUPcontains%ddifferentgroups.'
ngroups)
%convertto1,-1.
g=1-(2*(g-1));
%handleoptionalarguments
ifnumoptargs>
=1
ifrem(numoptargs,2)==1
IncorrectNumberOfArguments'
Incorrectnumberofargumentsto%s.'
mfilename);
okargs={'
kernel_function'
'
method'
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)
UnknownParameterName'
Unknownparametername:
%s.'
pname);
elseiflength(k)>
1
AmbiguousParameterName'
Ambiguousparametername:
switch(k)
case1%kernel_function
ifischar(pval)
okfuns={'
radial'
funNum=strmatch(lower(pval),okfuns);
ifisempty(funNum)
funNum=0;
switchfunNum%maybemakethislessstrictinthefuture
case1
case2
kfun=@quadratic_kernel;
case{3,4}
kfun=@rbf_kernel;
case5
kfun=@poly_kernel;
usePoly=true;
case6
kfun=@mlp_kernel;
useMLP=true;
otherwise
UnknownKernelFunction'
UnknownKernelFunction%s.'
kfun);
elseifisa(pval,'
function_handle'
kfun=pval;
BadKernelFunction'
Thekernelfunctioninputdoesnotappeartobeafunctionhandle\norvalidfunctionname.'
case2%method
ifstrncmpi(pval,'
qp'
2)
ifisempty(which('
warning('
NoOptim'
TheOptimizationToolboxisrequiredtousethequadraticprogrammingmethod.'
elseifstrncmpi(pval,'
ls'
UnknownMethod'
Unknownmethodoption%s.Validmethodsare'
and'
pval);
case3%display
ifpval~=0
ifsize(training,2)==2
plotflag=true;
OnlyPlot2D'
Thedisplayoptioncanonlyplot2Dtrainingdata.'
case4%kfunargs
ifiscell(pval)
kfunargs=pval;
kfunargs={pval};
case5%quadprog_opts
ifisstruct(pval)
qp_opts=pval;
elseifiscell(pval)
qp_opts=optimset(pval{:
});
BadQuadprogOpts'
QUADPROG_OPTSmustbeanoptsstructure.'
case6%polyorder
if~isscalar(pval)||~isnumeric(pval)
BadPolyOrder'
POLYORDERmustbeascalarvalue.'
ifpval~=floor(pval)||pval<
1
PolyOrderNotInt'
Theorderofthepolynomialkernelmustbeapositiveinteger.'
setPoly=true;
case7%mlpparams
ifnumel(pval)~=2
BadMLPParams'
MLP_PARAMSmustbeatwoelementarray.'
if~isscalar(pval
(1))||~isscalar(pval
(2))
MLPParamsNotScalar'
Theparametersofthemulti-layerperceptronkernelmustbescalar.'
kfunargs={pval
(1),pval
(2)};
setMLP=true;
ifsetPoly&
~usePoly
PolyOrderNotPolyKernel'
Youspecifiedapolynomialorderbutnotapolynomialkernel'
ifsetMLP&
~useMLP
MLPParamNotMLPKernel'
YouspecifiedMLPparametersbutnotanMLPkernel'
%plotthedataifrequested
ifplotflag
[hAxis,hLines]=svmplotdata(training,g);
legend(hLines,cellstr(groupString));
%calculatekernelfunction
try
kx=feval(kfun,training,training,kfunargs{:
%ensurefunctionissymmetric
kx=(kx+kx'
)/2;
catch
Errorcalculatingthekernelfunction:
\n%s\n'
lasterr);
%createHessian
%addsmallconstanteyetoforcestability
H=((g*g'
).*kx)+sqrt(eps(class(training)))*eye(n);
ifuseQuadprog
%Thelargescalesolvercannothandlethistypeofproblem,soturnit
%off.
qp_opts=optimset(qp_opts,'
Large