Main Content

view

View classification tree

Description

example

view(tree) returns a text description of tree, a decision tree.

example

view(tree,Name,Value) describes tree with additional options specified by one or more Name,Value pair arguments.

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');

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

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

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

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');

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

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

Classification tree, specified as a ClassificationTree or CompactClassificationTree model object. That is, tree is a classification model returned by fitctree or compact.

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: view(Mdl,Mode="graph")

Display of tree, specified as 'graph' or 'text'. 'graph' opens a user interface displaying tree, and containing controls for querying the tree. 'text' sends output to the Command Window describing 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