Main Content

prune

Produce sequence of classification subtrees by pruning classification tree

Description

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

tree1 = prune(tree,Name=Value) specifies additional options using one or more name-value arguments. For example, you can specify the pruning criterion and which branch nodes to turn into leaf nodes.

example

Examples

collapse all

Construct and display a full classification tree for Fisher's iris data.

load fisheriris;
varnames = {'SL','SW','PL','PW'};
t1 = fitctree(meas,species,'MinParentSize',5,'PredictorNames',varnames);
view(t1,'Mode','graph');

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

Construct and display the next largest tree from the optimal pruning sequence.

t2 = prune(t1,'Level',1);
view(t2,'Mode','graph');

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

Input Arguments

collapse all

Classification tree model, specified as a ClassificationTree model object trained with fitctree.

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,Criterion="error") sets the pruning level to 3 and the pruning criterion to "error".

Pruning criterion, specified as "error" or "impurity".

  • If you specify "error", then the cost of each node is the resubstitution error for the node multiplied by the probability for the node.

  • If you specify "impurity", and you specify PruneCriterion="impurity" and either SplitCriterion="gdi" or SplitCriterion="deviance" when you create tree with fitctree, then the cost of each node is impurity for the node multiplied by the probability for the node. You cannot specify "impurity" if you specify SplitCriterion="twoing" when you create tree.

.

Example: Criterion="impurity"

Data Types: char | string

Pruning cost, specified as a numeric scalar. When you specify this option, the prune function prunes tree to the specified value of the pruning cost.

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

Example: Alpha=2

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

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=4

Data Types: single | double

Output Arguments

collapse all

Updated version of tree, returned as a ClassificationTree 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 fitctree 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 fitctree.

Extended Capabilities

Version History

Introduced in R2011a