简体   繁体   中英

Using and understanding MATLAB's TreeBagger (a random forest) method

I'm trying to use MATLAB's TreeBagger method, which implements a random forest.

I get some results, and can do a classification in MATLAB after training the classifier. However I'd like to "see" the trees, or want to know how the classification works.

For example, let's run this minimal example, I found here: Matlab treebagger example

So, I end up with a classificator stored in "B". How can I inspect the trees? Like having a look at each node, to see on which criteria (eg feature) the decision is made? Entering B returns:

B = 

  TreeBagger
Ensemble with 20 bagged decision trees:
           Training X:                [6x2]
           Training Y:                [6x1]
               Method:       classification
                Nvars:                    2
         NVarToSample:                    2
              MinLeaf:                    1
                FBoot:                    1
SampleWithReplacement:                    1
 ComputeOOBPrediction:                    0
     ComputeOOBVarImp:                    0
            Proximity:                   []
           ClassNames:             '0'             '1'

I can't see something like B.trees or so.

And a follow-up question would be: How to port your random-forest code you prototyped in MATLAB to any other language. Then you need to know how each tree works, so you can implement it in the target language.

I hope you get the point, or understand my query ;)

Thanks for answers!

Best, Patrick

Found out how to inspect the trees, by running the view() command. Eg for inspecting the first tree of the example:

>> view(B.Trees{1})
Decision tree for classification
1 if x2<650 then node 2 elseif x2>=650 then node 3 else 0
2 if x1<4.5 then node 4 elseif x1>=4.5 then node 5 else 1
3 class = 0
4 class = 0
5 class = 1

By passing some more arguments to the view() command, the tree can also be visualized:

view(B.Trees{1},'mode','graph')

在此处输入图片说明

to view multiple trees just use loop :

for n=1:30 %number of tree
view(t.Trees{n});
end

you can find the source here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM