fitctree
statistics: Mdl = fitctree (X, Y)
statistics: Mdl = fitctree (…, name, value)
Fit a binary decision tree for classification.
Mdl = fitctree (X, Y) grows a binary decision tree
on the predictor data X and the class labels Y, and returns it
as a ClassificationTree object.
An observation whose class label is missing is dropped, and the rows kept
are reported in RowsUsed. An observation missing some of its
predictors is kept: it descends the tree as far as the predictors it does
carry allow and is answered there.
Mdl = fitctree (…, name, value) takes the
options below.
| Name | Value |
|---|---|
'AlgorithmForCategorical' | How a node with three or more
classes splits a categorical predictor: 'exact',
'pullleft', 'pca' or 'ovabyclass'. By default the
exact search is taken when the node holds at most 'MaxNumCategories'
levels, and otherwise the best split of 'ovabyclass',
'pca' and 'pullleft', leaving 'ovabyclass' out above
four classes. |
'CategoricalPredictors' | The predictors whose values are
levels, as indices, as a logical vector with one element per predictor, or
as 'all'. Such a predictor is split into two sets of levels, and an
observation whose level a node did not see stops there. |
'ClassNames' | The classes to fit, of the same type as Y. Observations of any other class are dropped. The model keeps the classes in this order; by default they are sorted. |
'Cost' | A square matrix with one row and column per
class, where element (i,j) is the cost of classifying an
observation of class i into class j, or a structure with
fields ClassNames and ClassificationCosts. The default is
1 - eye (K). A non-default cost changes the shape of the tree, not
only what it predicts. |
'MaxNumCategories' | A nonnegative integer, the most levels a node with three or more classes searches exactly by default. The default is 10. |
'MaxNumSplits' | A nonnegative integer, the largest number of branch nodes the tree may take. The default is one less than the number of observations. |
'MergeLeaves' | 'on' (default) or 'off'.
When on, a pair of leaves whose parent is no worse than the two of them
together is merged back into that parent. |
'MinLeafSize' | A positive integer, the fewest observations a leaf may hold. The default is 1. |
'MinParentSize' | A positive integer, the fewest
observations a node must hold to be split. The default is 10. The value
the fit uses is max (MinParentSize, 2 * MinLeafSize). |
'NumVariablesToSample' | A positive integer, the number
of predictors each split is chosen from, drawn afresh at every node, or
'all' (default). A number no smaller than the number of
predictors samples them all and is reported as 'all'. MATLAB also
accepts a fractional number, which is refused here. |
'PredictorNames' | A cell array of character vectors naming the columns of X. |
'Prior' | 'empirical' (default),
'uniform', a numeric vector with one element per class, or a
structure with fields ClassNames and ClassProbs. |
'Prune' | 'on' (default) or 'off'. When
on, the cost complexity pruning sequence is estimated and reported in
PruneList and PruneAlpha. The tree returned is the unpruned
one either way; prune takes a subtree out of the sequence. |
'PruneCriterion' | 'error', the only criterion
implemented. |
'ResponseName' | A character vector naming the response.
The default is 'Y'. |
'ScoreTransform' | A character vector naming a transform
to apply to the scores, or a function handle. The default is
'none'. |
'SplitCriterion' | 'gdi' (default), the Gini
diversity index, or 'deviance', the cross entropy. |
'Weights' | A nonnegative numeric vector with one element per observation. The default is uniform. |
Source Code: fitctree
Surrogate splits and the 'twoing' split criterion are not
implemented, and an option asking for one of them is refused rather than
quietly ignored. On a node with three or more classes and more than
'MaxNumCategories' levels, the heuristic splits, the choice between
equally good partitions and which side each set of levels takes may differ
from MATLAB’s.
See also: ClassificationTree, treetrain, treepredict
Source Code: fitctree
Grow a classification tree on Fisher's iris data and look at it
load fisheriris Mdl = fitctree (meas, species);
The tree as text: a branch names its cut, a leaf names its class
view (Mdl);
Decision tree for classification 1 if x3<2.45 then node 2 elseif x3>=2.45 then node 3 else setosa 2 class = setosa 3 if x4<1.75 then node 4 elseif x4>=1.75 then node 5 else versicolor 4 if x3<4.95 then node 6 elseif x3>=4.95 then node 7 else versicolor 5 class = virginica 6 if x4<1.65 then node 8 elseif x4>=1.65 then node 9 else versicolor 7 class = virginica 8 class = versicolor 9 class = virginica
How much each predictor contributed
predictorImportance (Mdl)
ans =
0 0 0.090748 0.068213
The resubstitution error of the whole tree and of its subtrees
resubLoss (Mdl)
ans = 0.020000
Prune a tree back and watch the error rise as it gets smaller
load fisheriris Mdl = fitctree (meas, species); levels = 0:numel (Mdl.PruneAlpha) - 1; leaves = zeros (size (levels)); err = zeros (size (levels)); for ii = 1:numel (levels) sub = prune (Mdl, 'Level', levels(ii)); leaves(ii) = sum (! sub.IsBranchNode); err(ii) = resubLoss (sub); endfor [leaves(:), err(:)]
ans = 5.000000 0.020000 4.000000 0.026667 3.000000 0.040000 2.000000 0.333333 1.000000 0.666667