Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can extract the RandomForest from R for use in production?

I have a successful randomforest model, and I want to integrate it in another software, I know that I can use some libraries (like fastRF in Java o ALGLIB's DecisionForest for other languages) but how I can use the "model" trained in R? I have to re-train it in the new language?

Another view is extract it somehow, but I dont't know how to do it...

Any help will be appreciated

Thanks in advance

like image 517
nanounanue Avatar asked Mar 17 '12 22:03

nanounanue


2 Answers

Take a look at the pmml package which generate PMML for various models, RandomForest included. A basic example:

#?randomForest
library(randomForest)
library(pmml)
set.seed(131)
ozone.rf <- randomForest(Ozone ~ ., data=airquality, mtry=3,importance=TRUE, na.action=na.omit)
print(ozone.rf)
ozone.rf.pmml <- pmml(ozone.rf)
like image 189
Paolo Avatar answered Oct 17 '22 07:10

Paolo


The randomForest object has all the information about each tree in the object. Each tree is not particularly complicated, though it can be confusing.

iris.rf <- randomForest(Species ~ ., data=iris, importance=TRUE,
                         proximity=TRUE)
> names(iris.rf$forest)
  [1] "ndbigtree"  "nodestatus" "bestvar"    "treemap"    "nodepred"  
  [6] "xbestsplit" "pid"        "cutoff"     "ncat"       "maxcat"    
  [11] "nrnodes"    "ntree"      "nclass"     "xlevels"   

To work out how to use the forest outside of R, you'll have to look at the source code. Download the source package of randomForest, extract the tar.gz and look in the src directory. In rf.c you will see the function classForest (and for regression look at regForest in regrf.c). Look at the R function predict.randomForest to see how its called. You might have to use getAnywhere("predict.randomForest") to see it within R.

It will require a fair bit of mucking around to extract the R information and predict in another package, so you'd have to think carefully before you actually did this. Refitting in the software you intend to use may be more straightforward.

like image 33
rjad Avatar answered Oct 17 '22 07:10

rjad