Main Content

prune

Produce sequence of regression subtrees by pruning regression tree

Description

tree1 = prune(tree,) returns a copy of the regression tree tree that includes its optimal pruning sequence.

___ = prune(tree,Name=Value) specifies one additional option using a name-value argument. For example, you can specify to prune tree to a specific level.

example

Examples

collapse all

Load the carsmall data set. Consider Horsepower and Weight as predictor variables.

load carsmall;
X = [Weight Horsepower];
varNames = ["Weight" "Horsepower"];

Grow a regression tree using the entire data set. View the tree.

Mdl = fitrtree(X,MPG,PredictorNames=varNames)
Mdl = 
  RegressionTree
           PredictorNames: {'Weight'  'Horsepower'}
             ResponseName: 'Y'
    CategoricalPredictors: []
        ResponseTransform: 'none'
          NumObservations: 94


view(Mdl,Mode="graph");

Figure Regression tree viewer contains an axes object and other objects of type uimenu, uicontrol. The axes object contains 60 objects of type line, text. One or more of the lines displays its values using only markers

The regression tree has 16 pruning levels.

Prune the regression tree to pruning-level 10. View the pruned tree.

MdlPruned = prune(Mdl,Level=10);
view(MdlPruned,Mode="graph");

Figure Regression tree viewer contains an axes object and other objects of type uimenu, uicontrol. The axes object contains 24 objects of type line, text. One or more of the lines displays its values using only markers

The pruned tree has six pruning levels.

Input Arguments

collapse all

Regression tree model, specified as a RegressionTree model object trained with fitrtree.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: prune(tree,Level=3) specifies to use prune tree to the third level.

Pruning cost, specified as a numeric scalar from 0 (no pruning) to 1 (prune to one node). The prune function prunes the tree to minimize the sum of (Alpha times the number of leaf nodes) and a cost (mean squared error).

If you specify Alpha, you cannot specify Level or Nodes.

Example: Alpha=0.1

Data Types: single | double

Pruning level, specified as a numeric scalar from 0 (no pruning) to the largest pruning level of the tree max(tree.PruneList). The prune function returns the tree pruned to the specified level.

If you specify Level, you cannot specify Alpha or Nodes.

Example: Level=3

Data Types: single | double

Branch nodes to turn into leaf nodes, specified as a numeric vector with elements from 1 to tree.NumNodes. Any tree branch nodes listed in Nodes become leaf nodes in tree1, unless their parent nodes are also pruned.

If you specify Nodes, you cannot specify Alpha or Level.

Example: Nodes=[1,3,5]

Data Types: single | double

Output Arguments

collapse all

Updated version of tree, returned as a RegressionTree model object.

When you specify any name-value arguments for prune, tree1 is a pruned tree created from tree using the optimal pruning sequence.

If you do not specify any name-value arguments for prune, tree1 is the full, unpruned tree, but with optimal pruning information added. This information is useful if you create tree by pruning another tree, or by using the fitrtree function with Prune="off",MergeLeaves="off". If you plan to prune a tree multiple times using the optimal pruning sequence, specify Prune="on" when you create tree with fitrtree.

Extended Capabilities

Version History

Introduced in R2011a