2-31 发表于 2006-5-24 11:47

急!! 请大家帮我调调这个程序

请各位帮忙调试这段程序<BR>数据:<BR>train_features<BR>
<TABLEcellSpacing=0 cellPadding=0 width=288 border=0 x:str>
<COLGROUP>
<COL style="WIDTH: 54pt" span=4 width=72>

<TRheight=19>
<TDalign=right width=72 height=19 x:num><FONT face=宋体>1</FONT></TD>
<TDalign=right width=72 x:num><FONT face=宋体>1</FONT></TD>
<TDalign=right width=72 x:num><FONT face=宋体>1</FONT></TD>
<TDalign=right width=72 x:num><FONT face=宋体>1</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>1</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>3</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>3</FONT></TD>
<TDalign=right x:num><FONT face=宋体>3</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>3</FONT></TD>
<TDalign=right x:num><FONT face=宋体>3</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>3</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>1</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>1</FONT></TD>
<TDalign=right x:num><FONT face=宋体>3</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>3</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>1</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>3</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD></TR></TABLE>targets<BR>
<TABLEcellSpacing=0 cellPadding=0 width=72 border=0 x:str>
<COLGROUP>
<COL style="WIDTH: 54pt" width=72>

<TRheight=19>
<TDalign=right width=72 height=19 x:num><FONT face=宋体>1</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>1</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>2</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>2</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>2</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>1</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>2</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>1</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>2</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>2</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>2</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>2</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>2</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>1</FONT></TD></TR></TABLE>prediction_features<BR>
<TABLEcellSpacing=0 cellPadding=0 width=288 border=0 x:str>
<COLGROUP>
<COL style="WIDTH: 54pt" span=4 width=72>

<TRheight=19>
<TDalign=right width=72 height=19 x:num><FONT face=宋体>3</FONT></TD>
<TDalign=right width=72 x:num><FONT face=宋体>3</FONT></TD>
<TDalign=right width=72 x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right width=72 x:num><FONT face=宋体>2</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>3</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>1</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>1</FONT></TD>
<TDalign=right x:num><FONT face=宋体>3</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD></TR>
<TRheight=19>
<TDalign=right height=19 x:num><FONT face=宋体>3</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>2</FONT></TD>
<TDalign=right x:num><FONT face=宋体>1</FONT></TD></TR></TABLE>
<P>程序:<BR>function D=dan(train_features,train_targets,prediction_features)</P>
<P>%输入:train_features,train_targets,prediction_features<BR>%输出:D<BR>=size(train_features);<BR>Nbins=length(unique(train_features));<BR>tree=build_tree(train_features, train_targets, Nbins);<BR>%使用树 <BR>targets=use_tree(prediction_features, 1:size(prediction_features,1), tree, Nbins, unique(train_targets));</P>
<P>D=reshape(targets,[],1);<BR>%END</P>
<P>function targets=use_tree(features, indices, tree, Nbins, Uc)<BR>%使用树:如果模型已经得到,对于预测集里的每一条记录,从树的根结点开始,向下找出该记录所属的类<BR>targets=zeros(1,size(features,1)); <BR>if (size(features,2)==1), %<BR>   for i=1:Nbins,<BR>       in=indices(find(features(indices)==i)); <BR>       if ~isempty(in), <BR>          if isfinite(tree.child(i)), <BR>             targets(in)=tree.child(i);<BR>          else<BR>            n=1+floor(rand(1)*length(Uc));<BR>            targets(in)=Uc(n);<BR>          end<BR>      end<BR>end<BR>return<BR>end<BR>dim=tree.split_dim;<BR>dims=find(~ismember(1:size(features,2),dim));<BR>%从树的根结点开始,向下找出记录所属的类<BR>for i=1:Nbins, <BR>    in=indices(find(features(indices,dim)==i)); <BR>    targets=targets+use_tree(features(:,dims),in,tree.child(i),Nbins,Uc);<BR>end<BR>%END use_tree</P>
<P>function tree=build_tree(features,targets,Nbins)</P>
<P>=size(features); <BR>Uc=unique(targets); <BR>U=length(unique(targets)); <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>                            = 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</P>
<P>for a=1:U,<BR>   Pnode(a)=length(find(targets==a))/Ni; <BR>end<BR>Inode=-sum(Pnode.*log(Pnode)/log(2)); </P>
<P>delta_Ib=zeros(1,L); <BR>P=zeros(1,U); <BR> <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>   <BR>      delta_Ib(a)=Inode-info; %H(U)-H(U|V)<BR>end</P>
<P> =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); %<BR>   if (~isempty(indices)),<BR>         tree.child(a)=build_tree(features(indices,dims),targets(indices),Nbins); <BR>   end<BR> end<BR> <BR></P>

lxq 发表于 2006-5-24 14:00

=size(train_features);<br>??? Input argument "train_features" is undefined.<br><br>没定义变量名?
[此贴子已经被作者于2006-5-24 14:01:14编辑过]

2-31 发表于 2006-5-24 14:31

定义了呀,train_features就是上面给的那个数据集
在命令窗口输入:

dan(train_features,train_targets,prediction_features)

提示
??? Subscript indices must either be real positive integers or logicals.
Error in ==> D:\matlab\work\dan.m (use_tree)
On line 38 ==> in=indices(find(features(indices,dim)==i));
Error in ==> D:\matlab\work\dan.m (use_tree)
On line 39 ==> targets=targets+use_tree(features(:,dims),in,tree.child(i),Nbins,Uc);
Error in ==> D:\matlab\work\dan.m
On line 12 ==> targets=use_tree(prediction_features, 1:size(prediction_features,1), tree, Nbins, unique(train_targets));

怎么处理啊??
循环变量和向量索引值都没有给负值,为什么还会出现
??? Subscript indices must either be real positive integers or logicals.

[ 本帖最后由 ChaChing 于 2009-11-3 15:10 编辑 ]
页: [1]
查看完整版本: 急!! 请大家帮我调调这个程序