I want to display the quinlan decision tree C4.5 but the program shows me a binary tree !!! What is the problem ?
5 views (last 30 days)
Show older comments
function node=buildtree(rows,scoref)
if( size(rows,1)==1 || size(rows(:,end),1)==1 || size(unique(rows(:,end)),1)==1 )
node=decisionnode(-1,{},uniquecounts(rows));
return
end
if nargin <2
scoref=@entropy;
end
current_score=scoref(rows);
% Set up some variables to track the best criteria
best_gain=0.0;
best_criteria={};
best_sets={};
% P=uniquecounts(rows);
% P=cell2mat(reshape(P(:,2),[1,size(P(:,2),1)]));
% P=(P/size(rows,1));
% SplitInfo=sum(-P.*log(eps+P)/log(2));
for col=1:size(rows,2)-1
SplitInfo=0.0;
% Generate the list of different values in this column
if iscellstr(rows(:,col))
is_cell_str=1;
column_values=sortrows(unique(rows(:,col)),-1);
Informations=column_values;
else
is_cell_str=0;
column_values=unique(cell2mat(rows(:,col)));
end
for v=1:size(column_values,1)
if is_cell_str
set=divideset(rows,col,column_values{v});
else
set=divideset(rows,col,column_values(v));
end
% Information gain
p=size(set{1,1},1)/size(rows,1);
SplitInfo=-p.*log(eps+p)/log(2)+SplitInfo;
% gain=current_score-p*scoref(set{1,1})-(1-p)*scoref(set{1,2});
train=set{1,1};
Informations{v,2}=Information(train,scoref)*p;
end
Info=current_score-sum( cell2mat(Informations(:,2)'));
gain_ratio=Info/SplitInfo;
%GainRatios=GainRatio(rows,current_score);
%f=find(GainRatios==max(GainRatios));
if gain_ratio>best_gain && size(set{1,1},1)>0 && size(set{1,2},1)>0
best_gain=gain_ratio;
load data_label;
if is_cell_str
% best_criteria={col,column_values{v},data_label{col}};
best_criteria={col,data_label{col}};
else
best_criteria={col,column_values(v)};
end
best_sets=set;
end
end
if best_gain>0
f1=best_sets{1,1}
trueBranch=buildtree(best_sets{1,1},scoref);
f=best_sets{1,2}
falseBranch=buildtree(best_sets{1,2},scoref);
node=decisionnode(best_criteria{1},best_criteria{2},{},trueBranch,falseBranch)
else
node=decisionnode(-1,{},uniquecounts(rows))
end
end
0 Comments
Answers (0)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!