prune
Produce sequence of regression subtrees by pruning regression tree
Description
___ = prune(
specifies one additional option using a name-value argument. For example, you can specify to
prune tree
,Name=Value
)tree
to a specific level.
Examples
Prune Regression Tree
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
tree
— Regression tree model
RegressionTree
model object
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.
Alpha
— Pruning cost
numeric scalar
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
Level
— Pruning level
numeric scalar
Nodes
— Branch nodes to turn into leaf nodes
numeric vector
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
tree1
— Updated version of tree
RegressionTree
model object
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
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
prune
does not execute pruning (estimation of the optimal sequence of pruned subtrees) on a GPU.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced in R2011a
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)