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

    %% Compare In-Sample Posterior Probabilities for Each Subtree
%%
% Load Fisher's iris data set. Partition the data into training (50%) 

% Copyright 2015 The MathWorks, Inc.

load fisheriris
%%
% Grow a classification tree using the all petal measurements.
Mdl = fitctree(meas(:,3:4),species);
n = size(meas,1); % Sample size
K = numel(Mdl.ClassNames); % Number of classes
%%
% View the classification tree.
view(Mdl,'Mode','graph');
%%
% The classification tree has four pruning levels. Level 0 is the full,
% unpruned tree (as displayed). Level 4 is just the root node (i.e., no
% splits).
%%
% Estimate the posterior probabilities for each class using the subtrees
% pruned to levels 1 and 3.
[~,Posterior] = resubPredict(Mdl,'SubTrees',[1 3]);
%%
% |Posterior| is an |n|-by- |K|-by- 2 array of
% posterior probabilities.  Rows of |Posterior| correspond to observations,
% columns correspond to the classes with order |Mdl.ClassNames|, and pages
% correspond to pruning level.
%%
% Display the class posterior probabilities for iris 125 using
% each subtree.
Posterior(125,:,:)
%%
% The decision stump (page 2 of |Posterior|) has trouble predicting whether
% iris 125 is versicolor or virginica.