Predict resubstitution labels of classification tree
label = resubPredict(tree)
[label,posterior] = resubPredict(tree)
[label,posterior,node] = resubPredict(tree)
[label,posterior,node,cnum] = resubPredict(tree)
[label,...] = resubPredict(tree,Name,Value)
A classification tree constructed by
comma-separated pairs of
the argument name and
Value is the corresponding value.
Name must appear inside quotes. You can specify several name and value
pair arguments in any order as
Matrix or array of posterior probabilities for classes
The node numbers of
The class numbers that
Find the total number of misclassifications of the Fisher iris data for a classification tree.
load fisheriris tree = fitctree(meas,species); Ypredict = resubPredict(tree); % The predictions Ysame = strcmp(Ypredict,species); % True when == sum(~Ysame) % How many are different?
ans = 3
Load Fisher's iris data set. Partition the data into training (50%)
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.
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
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.
ans = ans(:,:,1) = 0 0.0217 0.9783 ans(:,:,2) = 0 0.5000 0.5000
The decision stump (page 2 of
Posterior) has trouble predicting whether iris 125 is versicolor or virginica.
The posterior probability of the classification at a node is the number of training sequences that lead to that node with this classification, divided by the number of training sequences that lead to that node.
For example, consider classifying a predictor
Generate 100 random points and classify them:
rng(0) % For reproducibility X = rand(100,1); Y = (abs(X - .55) > .4); tree = fitctree(X,Y); view(tree,'Mode','graph')
Prune the tree:
tree1 = prune(tree,'Level',1); view(tree1,'Mode','graph')
The pruned tree correctly classifies observations that are less than 0.15
true. It also correctly classifies observations
between .15 and .94 as
false. However, it incorrectly
classifies observations that are greater than .94 as
false. Therefore the score for observations that are
greater than .15 should be about .05/.85=.06 for
and about .8/.85=.94 for
Compute the prediction scores for the first 10 rows of
[~,score] = predict(tree1,X(1:10)); [score X(1:10,:)]
ans = 10×3 0.9059 0.0941 0.8147 0.9059 0.0941 0.9058 0 1.0000 0.1270 0.9059 0.0941 0.9134 0.9059 0.0941 0.6324 0 1.0000 0.0975 0.9059 0.0941 0.2785 0.9059 0.0941 0.5469 0.9059 0.0941 0.9575 0.9059 0.0941 0.9649
Indeed, every value of
X (the rightmost column) that is
less than 0.15 has associated scores (the left and center columns) of
1, while the other values of
X have associated scores of
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).