声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2781|回复: 8

为什么我的M文件执行不了?

[复制链接]
发表于 2006-5-12 16:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
<P>function tree=j_tree(train_features,train_targets)%建树算法:<BR>[Ni,L]=size(train_features); %得到非类别属性训练集的大小Ni*L<BR>Nbins=length(unique(train_features)); %得到训练集的属性值的长度<BR>Uc=unique(train_targets); %得到类别属性目标集的值<BR>U=length(unique(train_targets)); %得到目标集的属性值的长度<BR>if (length(unique(train_targets))==1), %结束条件<BR>    tree.split_dim=0;<BR>    tree.ClassName=train_targets(U);<BR>    return<BR>end</P>
<P><BR>for i=1:U,<BR>     Pnode(i)=length(find(train_targets==i))/Ni; </P>
<P>end<BR>Inode=-sum(Pnode.*log(Pnode)/log(2)); </P>
<P><BR>delta_Ib=zeros(1,L);</P>
<P>P=zeros(1,U); <BR><BR>  for i=1:L, <BR>      for k=1:Nbins, <BR>          indices=find(train_features(:,i)==k); <BR>          V(k)=length(indices);<BR>          Y(k)=length(indices)/Ni;<BR>          %if (V(k)~=0), <BR>            for j=1:U, </P>
<P>                  s=length(find(train_targets(indices)==j)); </P>
<P>                  P(j)=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(i)=Inode-info; %H(U)-H(U|V)<BR>  end<BR>  <BR>[m,dim]=max(delta_Ib); <BR>             <BR>tree.split_dim=dim;<BR>dims=find(~ismember(1:L,dim)); </P>
<P>for i=1:Nbins,<BR>     indices=find( train_features(:,dim) ==i); </P>
<P>     tree.child(i)=j_tree(train_features(indices,dims),train_targets(indices)); </P>
<P>end<BR></P>
<P>这是我根据数据挖掘中的决策树ID3算法写的建树算法j_tree.m文件</P>
<P>从Import Data...给它Excel数据Sheet1(14*4),Sheet2(14*1)</P>
<P>在matlab命令窗口输入:</P>
<P>train_features=Sheet1;</P>
<P>train_targets=Sheet2;</P>
<P>j_tree(train_features,train_targets)</P>
<P>之后就出现了上面的提示</P>
<P 0cm 0pt?><FONT face="Times New Roman">??? Subscripted assignment between dissimilar structures.</FONT></P>
<P 0cm 0pt?>
<P><FONT face="Times New Roman"></FONT></P>
<P>
<P 0cm 0pt?><FONT face="Times New Roman">Error in ==&gt; D:\matlab\work\j_tree.m</FONT></P>
<P 0cm 0pt?><FONT face="Times New Roman">On line 57  ==&gt;  tree.child(i)=j_tree(train_features(indices,dims),train_targets(indices)); </FONT></P>
<P>除去最后的一个for循环,直接在命令窗口输入M文件的内容,整个过程都进行计算了, 能得到delta_Ib,m,dim等的值,且与书上所给的例子的计算值完全相同</P>
<P>当根据提示<FONT face="Times New Roman"> </FONT></P>
<P 0cm 0pt?><FONT color=#000000>??? Undefined function or variable 'Pnode'.</FONT></P>
<P 0cm 0pt?><FONT color=#000000>Error in ==&gt; D:\matlab\work\j_tree.m</FONT></P>
<P 0cm 0pt?><FONT color=#000000>On line 21  ==&gt;  Inode=-sum(Pnode.*log(Pnode)/log(2)); %信息熵H(U)=-sum P(U)log2P(U)</FONT></P>
<P>添加一句Pnode=zeros(1,U);之后,又出现</P>
<P>??? Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N)<BR>to change the limit.  Be aware that exceeding your available stack space can<BR>crash MATLAB and/or your computer.</P>
<P 0cm 0pt?><FONT face="Times New Roman">Error in ==&gt; D:\matlab\work\j_tree.m</FONT></P>
<P 0cm 0pt?><FONT face="Times New Roman">On line 6  ==&gt;  Uc=unique(train_targets); %得到类别属性目标集的值</FONT></P>
<P 0cm 0pt?><FONT face="Times New Roman">我该怎么办啊?<BR></P>
<P></FONT><BR></P>
回复
分享到:

使用道具 举报

发表于 2006-5-13 16:28 | 显示全部楼层

回复:(2-31)为什么我的M文件执行不了?

...
当根据提示
??? Undefined function or variable 'Pnode'.
Error in ==> D:\matlab\work\j_tree.m
On line 21 ==> Inode=-sum(Pnode.*log(Pnode)/log(2)); %信息熵H(U)=-sum P(U)log2P(U)

红色标记的部分是在什么情况下出现的?最好把你前面的数据给一下方便调试

[ 本帖最后由 ChaChing 于 2009-5-23 12:37 编辑 ]
 楼主| 发表于 2006-5-14 10:55 | 显示全部楼层
<P>在加上   %if (V(k)~=0), 之后出现<BR>数据  <BR>Sheet1:<BR>
<TABLE 216pt; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 width=288 border=0 x:str>
<COLGROUP>
<COL style="WIDTH: 54pt" span=4 width=72>

<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; WIDTH: 54pt; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right width=72 height=19 x:num>1</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; WIDTH: 54pt; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right width=72 x:num>1</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; WIDTH: 54pt; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right width=72 x:num>1</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; WIDTH: 54pt; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right width=72 x:num>1</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>1</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>1</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>1</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>2</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>2</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>1</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>1</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>1</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>3</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>2</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>1</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>1</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>3</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>3</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>2</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>1</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>3</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>3</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>2</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>2</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>2</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>3</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>2</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>2</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>1</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>2</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>1</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>1</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>1</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>3</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>2</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>1</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>3</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>2</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>2</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>1</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>1</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>2</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>2</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>2</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>2</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>2</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>1</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>2</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>2</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>1</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>2</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>1</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>3</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>2</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>1</TD>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" align=right x:num>2</TD></TR></TABLE>Sheet2:
<TABLE 54pt; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 width=72 border=0 x:str>
<COLGROUP>
<COL style="WIDTH: 54pt" width=72>

<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; WIDTH: 54pt; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right width=72 height=19 x:num>1</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>1</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>2</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>2</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>2</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>1</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>2</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>1</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>2</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>2</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>2</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>2</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>2</TD></TR>
<TR 14.25pt" height=19>
<TD #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; HEIGHT: 14.25pt; BACKGROUND-COLOR: transparent" align=right height=19 x:num>1</TD></TR></TABLE><BR></P>[/QUOTE]
 楼主| 发表于 2006-5-16 10:03 | 显示全部楼层
我这个程序到底是哪里出问题了???

要生成的可能是一棵多叉树,我想知道我的这个程序的递归用的对不对?

[ 本帖最后由 ChaChing 于 2009-5-23 12:42 编辑 ]
发表于 2006-5-16 20:58 | 显示全部楼层

回复:(2-31)为什么我的M文件执行不了?

没出现你上述说的问题,只有一条警告<BR><BR>Warning: Divide by zero.<BR>(Type "warning off MATLAB:divideByZero" to suppress this warning.)<BR>In D:\MATLAB6p5\work\j_tree.m at line 34
 楼主| 发表于 2006-5-17 16:14 | 显示全部楼层
<P>我把程序修改了一下,主要是在它的结束条件方面<BR>function tree=m_tree(features,targets,Nbins)</P>
<P>[Ni,L]=size(features); %得到非类别属性训练集的大小Ni*L<BR>Uc=unique(targets); %得到类别属性目标集的值  1 2 3 类<BR>U=length(unique(targets)); %得到目标集的属性值的长度3</P>
<P>%结束条件<BR>if (length(unique(targets))==1),%类别相同<BR>    tree.split_dim=0;<BR>    tree.child(a)=targets(U); %???想说明它是在哪个分枝出现,但不知道这个a如何给<BR>   return<BR>end</P>
<P>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</P>
<P>%开始计算<BR>for a=1:U,<BR>     Pnode(a)=length(find(targets==a))/Ni; <BR>end<BR>Inode=-sum(Pnode.*log(Pnode)/log(2)); <BR><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>      <BR>      delta_Ib(a)=Inode-info; %H(U)-H(U|V)<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)=m_tree(features(indices,dims),targets(indices),Nbins); %对既含正例又含反例的子集,递归调用建树算法<BR>     end<BR> end</P>
<P> 执行结果: split_dim=1<BR>           tree:[1*3 struct]<BR>中间都没有计算,递归也没实现<BR><BR>用的这个例子的计算最终应该是这种形式:<BR>                    split_dim:1<BR>                    /    |   \<BR>   split_dim:2          1    split_dim:<BR>   /       \                   /      \<BR>   1        2                 1        2<BR><BR><BR> 可能还是结束条件有问题吧,怎么样才能实现递归?</P>
 楼主| 发表于 2006-5-18 18:15 | 显示全部楼层
<P>为什么这个程序只做到 split_dim=1<BR>                     tree:[1*3 struct]<BR>就不再往下执行了?怎么才能实现递归?</P>
发表于 2006-5-18 20:26 | 显示全部楼层

回复:(2-31)为什么这个程序只做到 split_dim=1 ...

<DIV class=quote><B>以下是引用<I>2-31</I>在2006-5-18 18:15:00的发言:</B><BR>
<P>为什么这个程序只做到 split_dim=1<BR>                     tree:[1*3 struct]<BR>就不再往下执行了?怎么才能实现递归?</P></DIV>
<br>看一下判断条件是否有问题
发表于 2009-5-21 00:54 | 显示全部楼层

回复 楼主 2-31 的帖子

我最近也在用这个程序作东西,遇到了同样的问题,应该是因为你的树结构中中间结点包含structure 数组 child,但是叶子节点中没有child。因此当某些中间结点的子节点既有叶子又有中间结点时,递归时tree.child(a)=m_tree(...)的操作出错,因为它为数组tree.child的元素所附的值结构不同。

我采用的解决方法笨一点,直接在前面所有的终止条件语段中出了给split_dim赋值外,再加一句tree.child=[];就好了

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-11 19:53 , Processed in 0.092878 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表