Categories &

Functions List

Function Reference: fitrtree

statistics: Mdl = fitrtree (X, 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.

  • X must be a NxP numeric matrix of predictor data, where rows correspond to observations and columns to predictors.
  • Y must be a Nx1 numeric vector holding the response of each observation in X.

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.

NameValue
'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.
'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 numeric vector with one element per observation. The default is uniform.

Source Code: fitrtree

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