fitrtree
statistics: Mdl = fitrtree (X, Y)
statistics: Mdl = fitrtree (Tbl, ResponseVarName)
statistics: Mdl = fitrtree (Tbl, formula)
statistics: Mdl = fitrtree (Tbl, Y)
statistics: Mdl = fitrtree (…, name, value)
Fit a binary decision tree for regression.
Mdl = fitrtree (X, Y) grows a binary decision tree
on the predictor data X and the response Y, and returns it as a
RegressionTree object.
Mdl = fitrtree (Tbl, ResponseVarName) takes both
from the table Tbl: ResponseVarName names the variable holding
the response and every other variable is a predictor.
Mdl = fitrtree (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 = fitrtree (Tbl, Y) takes the predictors from
the table and the response 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 response 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 = fitrtree (…, name, value) takes the
options below.
| Name | Value |
|---|---|
'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, the
lower mean response on the left, and an observation whose level a node did
not see stops there. MATLAB does not always keep that side, and equally
good splits may be chosen differently.
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. |
'MaxNumCategories' | A nonnegative integer, recorded in
ModelParameters. The default is 10. Ordering the levels by their
mean response finds the best split whatever the number of levels. |
'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. |
'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' | 'mse', the only criterion a
regression tree has. |
'QuadraticErrorTolerance' | A positive scalar. A node whose squared error has fallen to this fraction of the root’s is not split further. The default is 1e-6. |
'ResponseName' | A character vector naming the response.
The default is 'Y'. |
'ResponseTransform' | A character vector naming a
transform to apply to the predicted response, or a function handle. The
default is 'none'. |
'SplitCriterion' | 'mse', the only criterion a
regression tree has. |
'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 the
predictions are double where MATLAB returns single. |
Surrogate splits are not implemented, and an option asking for them is refused rather than quietly ignored.
See also: RegressionTree, fitctree, treetrain, treepredict
Source Code: fitrtree
Grow a regression tree on the carsmall data and look at it
load carsmall X = [Weight, Cylinders, Horsepower]; Mdl = fitrtree (X, MPG, 'MinLeafSize', 15);
The tree as text: a branch names its cut, a leaf names what it fits
view (Mdl);
Decision tree for regression 1 if x1<3085.5 then node 2 elseif x1>=3085.5 then node 3 else 23.7181 2 if x3<89 then node 4 elseif x3>=89 then node 5 else 28.7931 3 if x1<3672 then node 6 elseif x1>=3672 then node 7 else 15.5417 4 if x1<2162 then node 8 elseif x1>=2162 then node 9 else 30.9375 5 fit = 24.0882 6 fit = 17.9333 7 fit = 13.8333 8 fit = 33.3056 9 fit = 29
How much each predictor contributed
predictorImportance (Mdl)
ans = 11.2532 0 1.4983
The mean squared error on the data it was fitted to
resubLoss (Mdl)
ans = 12.913
Prune a tree back and watch the error rise as it gets smaller
load carsmall X = [Weight, Cylinders, Horsepower]; Mdl = fitrtree (X, MPG, 'MinLeafSize', 15); 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.0000 12.9133
4.0000 14.4780
3.0000 16.4304
2.0000 22.3905
1.0000 63.8859
Fit from a table, and predict on one
load fisheriris
T = table (meas(:,2), meas(:,3), meas(:,4), meas(:,1), ...
'VariableNames', {'SW', 'PL', 'PW', 'SL'});
A column holding levels is a categorical predictor without being named one
T.Wide = categorical (meas(:,2) > 3, [false true], {'narrow', 'wide'});
The response is named by its column, and everything else is a predictor
Mdl = fitrtree (T, 'SL'); Mdl.PredictorNames
ans =
1x4 cell array
{'SW'} {'PL'} {'PW'} {'Wide'}
Mdl.ResponseName
ans = SL
Mdl.CategoricalPredictors
ans = 4
A model formula names them instead
Mdl2 = fitrtree (T, 'SL ~ PL + Wide'); Mdl2.PredictorNames
ans =
1x2 cell array
{'PL'} {'Wide'}
predict matches the table's variables by name, so their order does not matter and a column the model was not fitted on is passed over
yFit = predict (Mdl, T(1:5, [5, 3, 2, 1, 4])); yFit'
ans = 5.0333 4.6111 4.6111 4.8778 5.0333