Esta página aún no se ha traducido para esta versión. Puede ver la versión más reciente de esta página en inglés.

view

Sintaxis

view(tree)
view(tree,Name,Value)

Description

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

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

Argumentos de entrada

tree

A classification tree or compact classification tree created by fitctree or compact.

Argumentos de par nombre-valor

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

'Mode'

Value describing the display of tree, either '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.

Predeterminado: 'text'

Ejemplos

expandir todo

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

Sugerencias

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

Consulte también

|