资源描述:
《决策树ID3源程序》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、id3算法的matlab实现嚴帖]functionD=ID3(train_features,train_targets,params,region)%ClassifyusingQuinlan´sID3algorithm%Inputs:%features一Trainfeatures%targets-Traintargets%params-[Numberofbinsforthedata,Percentageofincorrectlyassignedsamplesatanode]%region-Deci
2、sionregionvector:[-xx-yynumber_of_points]%%Outputs%D-Decisionsufrace[Ni,M]=size(trainfeatures);%Getpcirameters[Nbins,incnode]=processparams(params);incnode=incnode*M/100;%ForthedecisionregionN=region(5);mx=ones(N,1)*linspace(region(l),region(2),N);myflatxy=
3、1inspace(region(3),region(4),N)´*ones(l,N):=[mx(:),my(:)]´;%Prcprocessing[f,t,UW,m]trainfeaturesflatxy=PCA(trainfeatures,traintargets,Ni,region);=UW*(trainfeatures一m*ones(l,M));;二UW*(flatxy-m*ones(l,N^2)):;%First,binthedataandthedecisionregionda
4、ta[II,binned_features]=high_histogram(train_features,Nbins,region);[II,binned_xy]=high^histogram(flatxy,Nbins,region);%BuiIdthetreerecursivelydisp(´Buildingtree´)tree=maketree(binnodfeatures,traintargets,incnode,Nbins):%Makethedecisionregionacco
5、rdingtothetreedisp(&acule;Buildingdecisionsurfaceusingthetree&scule;)targets=usetree(binnedxy,1:N2,tree,Nbins,unique(lrtiir)targets));D=reshape(targets,N,N);%ENDfunctiontargets=use_tree(features,indices,tree,Nbins,Uc)%Classifyrecursivelyusingatreetargets=ze
6、ros(i,size(features,2));if(size(features,i)—i),%0nlyonedimensionleft,soworkonitfori=1:Nbins,in二indices(find(features(indices)二二i));if"isempty(in),ifisfinite(troc.child(i)),targets(in)=tree・child(i);else%Nodatawasfoundinthetrainingsetforthisbin,sochooseitran
7、domallyn=1+floor(rand(l)*length(Uc));targets(in)二Uc(n);endendendbreakend%Thisisnotthelastlevelofthetree,so:%First,findthedimensionwearetoworkondim=tree・splitdim;dims=findCismember(1:size(features,1),dim)):classifyaccordingtoitfori=1:Nbins,in=indices(find(fe
8、atures(dim,indices)==i)):targets=targets+use_tree(features(dims,:),in,tree.child(i),Nbins,Uc);end%ENDusc^trcefunctiontree=make^tree(features,targets,inc_node,Nbins)%Buildatreerecursively[Ni,L]=size(fea