马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
请各位帮忙调试这段程序<BR>数据:<BR>train_features<BR>
<TABLE cellSpacing=0 cellPadding=0 width=288 border=0 x:str>
<COLGROUP>
<COL style="WIDTH: 54pt" span=4 width=72>
<TR height=19>
<TD align=right width=72 height=19 x:num><FONT face=宋体>1</FONT></TD>
<TD align=right width=72 x:num><FONT face=宋体>1</FONT></TD>
<TD align=right width=72 x:num><FONT face=宋体>1</FONT></TD>
<TD align=right width=72 x:num><FONT face=宋体>1</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>1</FONT></TD>
<TD align=right x:num><FONT face=宋体>1</FONT></TD>
<TD align=right x:num><FONT face=宋体>1</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>1</FONT></TD>
<TD align=right x:num><FONT face=宋体>1</FONT></TD>
<TD align=right x:num><FONT face=宋体>1</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>3</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>1</FONT></TD>
<TD align=right x:num><FONT face=宋体>1</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>3</FONT></TD>
<TD align=right x:num><FONT face=宋体>3</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>1</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>3</FONT></TD>
<TD align=right x:num><FONT face=宋体>3</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>3</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>1</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>1</FONT></TD>
<TD align=right x:num><FONT face=宋体>1</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>1</FONT></TD>
<TD align=right x:num><FONT face=宋体>3</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>1</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>3</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>1</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>1</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>1</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>1</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>1</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>3</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>1</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD></TR></TABLE>targets<BR>
<TABLE cellSpacing=0 cellPadding=0 width=72 border=0 x:str>
<COLGROUP>
<COL style="WIDTH: 54pt" width=72>
<TR height=19>
<TD align=right width=72 height=19 x:num><FONT face=宋体>1</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>1</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>2</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>2</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>2</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>1</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>2</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>1</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>2</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>2</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>2</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>2</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>2</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>1</FONT></TD></TR></TABLE>prediction_features<BR>
<TABLE cellSpacing=0 cellPadding=0 width=288 border=0 x:str>
<COLGROUP>
<COL style="WIDTH: 54pt" span=4 width=72>
<TR height=19>
<TD align=right width=72 height=19 x:num><FONT face=宋体>3</FONT></TD>
<TD align=right width=72 x:num><FONT face=宋体>3</FONT></TD>
<TD align=right width=72 x:num><FONT face=宋体>2</FONT></TD>
<TD align=right width=72 x:num><FONT face=宋体>2</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>3</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>1</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>1</FONT></TD>
<TD align=right x:num><FONT face=宋体>1</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>1</FONT></TD>
<TD align=right x:num><FONT face=宋体>3</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>1</FONT></TD></TR>
<TR height=19>
<TD align=right height=19 x:num><FONT face=宋体>3</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD>
<TD align=right x:num><FONT face=宋体>2</FONT></TD>
<TD align=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>[Ni,M]=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>[Ni,L]=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> [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>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> [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); %<BR> if (~isempty(indices)),<BR> tree.child(a)=build_tree(features(indices,dims),targets(indices),Nbins); <BR> end<BR> end<BR> <BR></P>[em06] |