[R] Dump decision trees of randomForest object

0 views
Skip to first unread message

Christian Sturz

unread,
Oct 9, 2008, 1:37:51 PM10/9/08
to r-h...@r-project.org
Hi,

I'm using the package randomForest to generate a classifier for the exemplary
iris data set:

data(iris)
iris.rf<-randomForest(Species~.,iris)

Is it possible to print all decision trees in the generated forest?
If so, can the trees be also written to disk?

What I actually need is to translate the decision trees in a random forest
into equivalent C++ if-then-else constructs to integrate them in a C++
project. Have this been done in the past and are there already any
implemented approaches/parser for that?

Cheers,
Chris
--

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Liaw, Andy

unread,
Oct 9, 2008, 2:38:44 PM10/9/08
to Christian Sturz, r-h...@r-project.org
See the getTree() function in the package. Also, the source package
contains C code that does the prediction that you may be able to work
from.

Andy

Notice: This e-mail message, together with any attachme...{{dropped:12}}

Christian Sturz

unread,
Oct 9, 2008, 4:30:26 PM10/9/08
to Liaw, Andy, r-h...@r-project.org
I've tried the getTree() function and printed a decision tree with print().
However, it seems to me that it's hard to parse this representation and
translate it into equivalent if-then-else C constructs. Are there no other
ways to dump the trees into a more hierarchical form?

What do you exactly mean with the prediction in the source package?

Maybe what I wanted to ask goes in the same direction: let's say I've learned
a random forest model from a learning set. Now I would like to use it in the
future as classifier to predict new examples. How can this be done? Can I save
a learned model and than invoke R with new examples and applied them to
the saved model without again training the random forest from scratch? If so,
please give me some hints how to do that.

Regards,
Chris

-------- Original-Nachricht --------
> Datum: Thu, 9 Oct 2008 14:38:44 -0400
> Von: "Liaw, Andy" <andy...@merck.com>
> An: "Christian Sturz" <linux...@gmx.net>, r-h...@r-project.org
> Betreff: RE: [R] Dump decision trees of randomForest object

> Notice: This e-mail message, together with any attach...{{dropped:15}}

Pedro.R...@sungard.com

unread,
Oct 9, 2008, 5:29:16 PM10/9/08
to linux...@gmx.net, andy...@merck.com, r-h...@r-project.org
Hi Chris,

Maybe it is easier if you try the following C++ library

http://mtv.ece.ucsb.edu/benlee/librf.html


Regards,


Pedro

Liaw, Andy

unread,
Oct 10, 2008, 10:48:58 AM10/10/08
to Christian Sturz, r-h...@r-project.org
If you just want to be able to use the trained RF model in some future R session for prediction on new data, just use save() to save the RF object, and load() it back in the future.

If you really want to write your own low-level code for prediction, you can take a look at the predictRegTree() function in randomForest/src/regTree.c (the last function in that file). It shows how prediction is done using the data structure from a randomForest object.

Andy

> attachments, contains
> > information of Merck & Co., Inc. (One Merck Drive,
> Whitehouse Station,
> > New Jersey, USA 08889), and/or its affiliates (which may be known
> > outside the United States as Merck Frosst, Merck Sharp & Dohme or
> > MSD and in Japan, as Banyu - direct contact information for
> affiliates is
> > available at http://www.merck.com/contact/contacts.html) that may be
> > confidential, proprietary copyrighted and/or legally
> privileged. It is
> > intended solely for the use of the individual or entity
> named on this
> > message. If you are not the intended recipient, and have
> received this
> > message in error, please notify us immediately by reply e-mail and
> > then delete it from your system.
>
> --
> Psssst! Schon vom neuen GMX MultiMessenger gehört? Der kann`s
> mit allen: http://www.gmx.net/de/go/multimessenger
>
Notice: This e-mail message, together with any attachme...{{dropped:12}}

Reply all
Reply to author
Forward
0 new messages