MATLAB: How to retrieve the levels of a node, maximum level and maximum pruning level from a CLASSREGTREE in MATLAB 7.6 (R2008a)

Statistics and Machine Learning Toolbox

I want to obtain the level of a node in a CLASSREGTREE, as well as the number of levels in the tree and the maximum pruning level.

Best Answer

There are at least two ways to obtain the level of a node in a CLASSREGTREE using MATLAB 7.6 (R2008a).
The first is to view the tree and read the number of levels from the GUI:
load fisheriris;
t = classregtree(meas,species,'names',{'SL' 'SW' 'PL' 'PW'});
view(t)
The second method allows you to programmatically obtain the number of levels. There is a file attached: 'levelOf.m' which uses recursion to obtain the level of any node in the tree, where the root node is level 1, the children of the root are level 2 and so forth. To obtain the number of levels, call LEVELOF with the following syntax:
nodeindex = t.numnodes;
nodelevel = levelOf(t,nodeindex)
To retrieve the maximum pruning level, use:
max(t.prunelist)