# prune

Produce sequence of regression subtrees by pruning regression tree

## Syntax

``tree1 = prune(tree)``
``___ = prune(tree,Alpha=alpha)``
``___ = prune(tree,Level=level)``
``___ = prune(tree,Nodes=nodes)``

## Description

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

example

````___ = prune(tree,Alpha=alpha)` specifies to prune `tree` to the pruning cost `alpha`.```

example

````___ = prune(tree,Level=level)` specifies to prune `tree` to a level `level`.```

example

````___ = prune(tree,Nodes=nodes)` specifies to turn the branch nodes listed in `nodes` into leaf nodes in `tree`.```

## 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");`

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");```

The pruned tree has six pruning levels.

## Input Arguments

collapse all

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

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).

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

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.

## 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`.

## Version History

Introduced in R2011a