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

    %% Prune a Classification Tree
% This example creates a classification tree for the |ionosphere| data, and
% prunes it to a good level.
%%
% Load the |ionosphere| data:

% Copyright 2015 The MathWorks, Inc.

load ionosphere
%%
% Construct a default classification tree for the data:
tree = fitctree(X,Y);
%%
% View the tree in the interactive viewer:
view(tree,'Mode','Graph')
%%
% Find the optimal pruning level by minimizing cross-validated loss:
[~,~,~,bestlevel] = cvLoss(tree,...
    'SubTrees','All','TreeSize','min')
%%
% Prune the tree to level |6|:
view(tree,'Mode','Graph','Prune',6)
%%
% Alternatively, use the interactive window to prune the tree.
%%
% The pruned tree is the same as the near-optimal tree in the
% "Select Appropriate Tree Depth" example.
%%
% Set |'TreeSize'| to |'SE'| (default) to find the maximal pruning level
% for which the tree error does not exceed the error from the best level
% plus one standard deviation:
[~,~,~,bestlevel] = cvLoss(tree,'SubTrees','All')
%%
% In this case the level is the same for either setting of |'TreeSize'|.
%%
% Prune the tree to use it for other purposes:
tree = prune(tree,'Level',6);
view(tree,'Mode','Graph')