[R] SOLVED: use classificators learned in R in "real-life", e.g. C

From: Lars <sich_at_gmx.de>
Date: Mon, 04 Feb 2008 12:49:27 +0100


>> I am interested in using R for machine learning (supervised
>> Currently, I have been investigating especially the rpart, tree, and
randomForest package, and have
>> achieved first results.
>> are there any experiences, how the learned classificators could
>> be used in e.g. C ?
>> in other words, I want to "transfer" the learned predictor from R
>> to C-code.
>You could use dput to write the representation to a file, and read it
>C. Parsing could be nasty, though, so I would prefer to extract the
>relevant information (e.g. fit$cptable and fit$splits in the rpart
>and write it to a database in numeric form with additional information
>required. Another option could be XML (package XML), and using one of
>many XML libraries in C(++).

thanks for your answer.
actually, taking a closer look at the representations of rpart or randomForest, I came to a much simpler and straight-forward solution: with only a couple of lines, the corresponding c-code for a tree can be emitted. e.g. for randomForest:

## generate C code from rforests. generate code for tree k

gen_code=function(rfobj,k) {

   visit=function(idx) {

     if (tree[idx,'status']==-1)
     else {
       cat(sprintf("if (GETVAR(%s)<=%f) {\n",tree[idx,'split 
var'],tree[idx,'split point']));
       visit(tree[idx,'left daughter']);
       cat("} else {\n");
       visit(tree[idx,'right daughter']);

   if (count != nrow(tree))

         stop("Invalid tree in ", deparse(substitute(rfobj))) }

however, if this is computed for 500 trees, maybe a more efficient way to compute the result for one tree could be necessary


R-help_at_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. Received on Mon 04 Feb 2008 - 12:02:43 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 04 Feb 2008 - 13:30:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive