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

Lars sich at gmx.de
Mon Feb 4 12:49:27 CET 2008


Hi,

 >> I am interested in using R for machine learning (supervised 
classification).
 >> 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 
 >from
 >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 
 >example),
 >and write it to a database in numeric form with additional information 
 >if
 >required. Another option could be XML (package XML), and using one of 
 >the
 >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) {

   tree=getTree(rfobj,k,labelVar=TRUE);
   count=0;
   loc.env=environment();
   visit=function(idx) {
     assign('count',count+1,env=loc.env);
     if (tree[idx,'status']==-1)
       cat(sprintf("RETURN(%s)\n",tree[idx,'prediction']))
     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']);
       cat("}\n");
     }
   }
   visit(1);
   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


regards
lars



More information about the R-help mailing list