www.gusucode.com > stats 源码程序 matlab案例代码 > stats/ComputeTheCostOfADecisionTreeExample.m

    %% Compute the Cost of a Decision Tree
% Find the best tree for Fisher's iris data using cross-validation.
%%
% Grow a large tree:

% Copyright 2015 The MathWorks, Inc.

load fisheriris;
t = classregtree(meas,species,...
                 'names',{'SL' 'SW' 'PL' 'PW'},...
                 'minparent',5)
view(t)
%%
% Find the minimum-cost tree:
rng(1); % For reproducibility
[c,s,n,best] = test(t,'crossvalidate',meas,species);
tmin = prune(t,'level',best)
view(tmin)
%%
% Plot the smallest tree within one standard error of the minimum cost tree:
[mincost,minloc] = min(c);
plot(n,c,'b-o',...
     n(best+1),c(best+1),'bs',...
     n,(mincost+s(minloc))*ones(size(n)),'k--')
xlabel('Tree size (number of terminal nodes)')
ylabel('Cost')
%%
% The solid line shows the estimated cost for each tree size, the dashed
% line marks one standard error above the minimum, and the square marks the
% smallest tree under the dashed line.