支持向量机的matlab代码Word格式.docx

上传人:b****1 文档编号:5782851 上传时间:2023-05-05 格式:DOCX 页数:15 大小:18.65KB
下载 相关 举报
支持向量机的matlab代码Word格式.docx_第1页
第1页 / 共15页
支持向量机的matlab代码Word格式.docx_第2页
第2页 / 共15页
支持向量机的matlab代码Word格式.docx_第3页
第3页 / 共15页
支持向量机的matlab代码Word格式.docx_第4页
第4页 / 共15页
支持向量机的matlab代码Word格式.docx_第5页
第5页 / 共15页
支持向量机的matlab代码Word格式.docx_第6页
第6页 / 共15页
支持向量机的matlab代码Word格式.docx_第7页
第7页 / 共15页
支持向量机的matlab代码Word格式.docx_第8页
第8页 / 共15页
支持向量机的matlab代码Word格式.docx_第9页
第9页 / 共15页
支持向量机的matlab代码Word格式.docx_第10页
第10页 / 共15页
支持向量机的matlab代码Word格式.docx_第11页
第11页 / 共15页
支持向量机的matlab代码Word格式.docx_第12页
第12页 / 共15页
支持向量机的matlab代码Word格式.docx_第13页
第13页 / 共15页
支持向量机的matlab代码Word格式.docx_第14页
第14页 / 共15页
支持向量机的matlab代码Word格式.docx_第15页
第15页 / 共15页
亲,该文档总共15页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

支持向量机的matlab代码Word格式.docx

《支持向量机的matlab代码Word格式.docx》由会员分享,可在线阅读,更多相关《支持向量机的matlab代码Word格式.docx(15页珍藏版)》请在冰点文库上搜索。

支持向量机的matlab代码Word格式.docx

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'

else

DataGroupSizeMismatch'

GROUPandTRAININGmusthavethesamenumberofrows.'

end

%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

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

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,'

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

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2