Main Content

view

View classification tree

Description

example

view(tree) returns a text description of the classification tree model tree.

example

view(tree,Mode=mode) specifies the display mode, either "graph" or "text".

Examples

collapse all

View textual and graphical displays of a trained classification tree.

Load Fisher's iris data set.

load fisheriris

Train a classification tree using all measurements.

Mdl = fitctree(meas,species);

View textual display of the trained classification tree.

view(Mdl)
Decision tree for classification
1  if x3<2.45 then node 2 elseif x3>=2.45 then node 3 else setosa
2  class = setosa
3  if x4<1.75 then node 4 elseif x4>=1.75 then node 5 else versicolor
4  if x3<4.95 then node 6 elseif x3>=4.95 then node 7 else versicolor
5  class = virginica
6  if x4<1.65 then node 8 elseif x4>=1.65 then node 9 else versicolor
7  class = virginica
8  class = versicolor
9  class = virginica

View graphical display of the trained classification tree.

view(Mdl,Mode="graph");

Load Fisher's iris data set.

load fisheriris

Grow a bag of 100 classification trees using all measurements.

rng(1) % For reproducibility
Mdl = TreeBagger(100,meas,species);

Alternatively, you can use fitcensemble to grow a bag of classification trees.

Mdl is a TreeBagger model object. Mdl.Trees stores the bag of 100 trained classification trees in a 100-by-1 cell array. That is, each cell in Mdl.Trees contains a CompactClassificationTree model object.

View a graph of the 10th classification tree in the bag.

Tree10 = Mdl.Trees{10};
view(Tree10,Mode="graph");

By default, the software grows deep trees for bags of trees.

Load Fisher's iris data set.

load fisheriris

Boost an ensemble of 100 classification trees using all measurements. Specify tree stumps as the weak learners.

t = templateTree('MaxNumSplits',1);
Mdl = fitcensemble(meas,species,'Method','AdaBoostM2','Learners',t);

Mdl is a ClassificationEnsemble model object. Mdl.Trained stores the ensemble of 100 trained classification trees in a 100-by-1 cell array. That is, each cell in Mdl.Trained contains a CompactClassificationTree model object.

View a graph of the 10th classification tree in the ensemble.

Tree10 = Mdl.Trained{10};
view(Tree10,'Mode','graph');

The graph shows a tree stump because you specified stumps as the weak learners for the ensemble. However, this behavior is not the default for fitcensemble. By default, fitcensemble grows shallow trees for boosted ensembles of trees. That is, 'Learners' is templateTree('MaxNumSplits',10).

Input Arguments

collapse all

Trained classification tree, specified as a ClassificationTree model object trained with fitctree, or a CompactClassificationTree model object created with compact.

Display of tree, specified as "text" or "graph". "text" displays output describing tree in the Command Window. "graph" opens a user interface that displays tree and contains controls for querying the tree.

Data Types: char | string

Tips

To view tree t from an ensemble of trees, enter one of these lines of code

view(Ens.Trained{t})
view(Bag.Trees{t})

  • Ens is a full ensemble returned by fitcensemble or a compact ensemble returned by compact.

  • Bag is a full bag of trees returned by TreeBagger or a compact bag of trees returned by compact.

To save tree in the Command Window, get a figure handle by using the findall and setdiff functions, and then save tree using the function saveas.

before = findall(groot,Type="figure"); % Find all figures
view(Mdl,Mode="graph")
after = findall(groot,Type="figure");
h = setdiff(after,before); % Get the figure handle of the tree viewer
saveas(h,"a.png")

Extended Capabilities

Version History

Introduced in R2011a