这个程序出来结果是: split_dim:1<BR>child[1*3 struct]<BR>我想让它显示:<BR>split_dim:1<BR>child(1) split_dim:1<BR>[1*2 struct]<BR>child(1,1)=2<BR>child(1,2)=1<BR>child(2) split_dim:2<BR>[1*2 struct]<BR>child(2,1)=1<BR>child(2,2)=2<BR>child(3) split_dim:0<BR>child(3)=1<BR>不知道该加个什么语句才能实现? 先谢谢大家!
<br>
<P>function tree=build_tree(features,targets,3)<BR>%建树:<BR>[Ni,L]=size(features); <BR>Uc=unique(targets);<BR>U=length(unique(targets)); <BR>%结束条件:<BR>if (length(unique(targets))==1)<BR>tree.split_dim=0;<BR>tree.child=targets(U); %???<BR>return<BR>end<BR>if (L == 1),<BR>for a = 1:Nbins,<BR>tree.split_dim = 0;<BR>indices = find(features == a);<BR>if ~isempty(indices),<BR>if (length(unique(targets(indices))) == 1),<BR>tree.child(a) = targets(indices(1));<BR>else<BR>H = hist(targets(indices), Uc);<BR>[m, T] = max(H);<BR>tree.child(a) = Uc(T);<BR>end<BR>else<BR>tree.child(a) = inf;<BR>end<BR>end<BR>return<BR>end<BR>for a=1:U,<BR>Pnode(a)=length(find(targets==a))/Ni; <BR>end<BR>Inode=-sum(Pnode.*log(Pnode)/log(2)); <BR>delta_Ib=zeros(1,L); <BR>P=zeros(1,U); <BR>for a=1:L, <BR>for k=1:Nbins, <BR>f=find(features(:,a)==k);<BR>V(k)=length(f);<BR>Y(k)=V(k)/Ni;<BR>if (V(k)~=0), <BR>for b=1:U, <BR>s=length(find(targets(f)==b)); <BR>P(b)=s/V(k); %P(s/v)<BR>end<BR>end<BR>Q=sum(-P.*log(eps+P)/log(2)); <BR>E(k)=Y(k).*Q; <BR>end <BR>info=sum(E); <BR>delta_Ib(a)=Inode-info; <BR>end<BR>[m,dim]=max(delta_Ib); <BR>tree.split_dim=dim;<BR>dims=find(~ismember(1:L,dim)); <BR>for a=1:Nbins,<BR>indices=find(features(:,dim) ==a); %把在Ak处取值相同的例子归于同一子集,取几个值就得几个子集<BR>if (~isempty(indices)),<BR>tree.child(a)=build_tree(features(indices,dims),targets(indices),Nbins); %对既含正例又含反例的子集,递归调用建树算法<BR>end<BR>end</P>
<P>features数据:<BR>1 1 1 1<BR>1 1 1 2<BR>2 1 1 1<BR>3 2 1 1<BR>3 3 2 1<BR>3 3 2 2<BR>2 3 2 2<BR>1 2 1 1<BR>1 3 2 1<BR>3 2 2 1<BR>1 2 2 2<BR>2 2 1 2<BR>2 1 2 1<BR>3 2 1 2<BR>targets 数据:<BR>1<BR>1<BR>2<BR>2<BR>2<BR>1<BR>2<BR>1<BR>2<BR>2<BR>2<BR>2<BR>2<BR>1</P> |