www.gusucode.com > 决策树matlab源码程序 > gain.m
%计算 information gain function rs=gain(data,n) x=length(data.sx); %属性数目 y=length(data.svalue); %样本数目 cel=tiqu(data,n); %输出第n属性的属性值 pos=length(cel); %记录属性值个数 for i=1:pos datpos=0;fod=0; for j=1:x if j~=n fod=fod+1; dat(i).data.sx{fod}=data.sx{j}; end end for j=1:y if isequal(cel{i},data.svalue{j}{n}) datpos=datpos+1;spos=0; for k=1:x if k~=n spos=spos+1; dat(i).data.svalue{datpos}{spos}=data.svalue{j}{k}; % end end end end cs(i)=ES(dat(i).data)*datpos/y; end rs.gain=ES(data)-sum(cs); rs.dat=dat; rs.cel=cel;