fitctree
statistics: Mdl = fitctree (X, Y)
statistics: Mdl = fitctree (Tbl, ResponseVarName)
statistics: Mdl = fitctree (Tbl, formula)
statistics: Mdl = fitctree (Tbl, 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.
Mdl = fitctree (Tbl, ResponseVarName) takes both
from the table Tbl: ResponseVarName names the variable holding
the class labels and every other variable is a predictor.
Mdl = fitctree (Tbl, formula) names them with a
model formula instead, 'Y ~ x1 + x2', which holds main effects
only and so takes no product, power or wildcard.
Mdl = fitctree (Tbl, Y) takes the predictors from
the table and the class labels from Y.
A table variable holding levels rather than numbers, which is a
categorical, a logical, a character array or a cell array of
character vectors, is a categorical predictor without being named one, and
'CategoricalPredictors' adds to that set rather than replacing it.
PredictorNames and ResponseName come from the table, and a
level is coded at prediction as it was coded at fitting, so a table given
to predict may hold only some of the levels.
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.
A predictor may be named rather than indexed, as a character matrix of one
padded name per row, a string array or a cellstr; a name must match an entry
of 'PredictorNames' exactly, its case included. |
'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 single or double vector with one
element per observation. The default is uniform. The model’s W keeps
the class of the weights, while every computation runs in double, so
Prior and the predictions are double where MATLAB returns single. |
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
Fit from a table, and predict on one
load fisheriris
T = table (meas(:,1), meas(:,2), meas(:,3), meas(:,4), ...
categorical (species), 'VariableNames', ...
{'SL', 'SW', 'PL', 'PW', 'Species'});
A column holding levels is a categorical predictor without being named one, so the width of the sepal is split into two and comes back as the fifth predictor rather than as a number
T.Wide = categorical (meas(:,2) > 3, [false true], {'narrow', 'wide'});
The response is named by its column, and everything else is a predictor
Mdl = fitctree (T, 'Species'); Mdl.PredictorNames
ans =
1x5 cell array
{'SL'} {'SW'} {'PL'} {'PW'} {'Wide'}
Mdl.CategoricalPredictors
ans = 5
A model formula names them instead, holding main effects only
Mdl2 = fitctree (T, 'Species ~ PL + Wide'); Mdl2.PredictorNames
ans =
1x2 cell array
{'PL'} {'Wide'}
predict reads a table by the names the model was fitted on, so the columns may come in any order and may carry more than the model needs
label = predict (Mdl, T(1:5, [6, 4, 3, 2, 1, 5])); label'
ans =
1x5 categorical array
setosa setosa setosa setosa setosa