5
$\begingroup$

say I have a sensor that measures temperature, pressure ++, and want to use this data to predict some quantity "A". If I use multivariate regression, I can simply implement a model of the form A=a0+a1x1+a2x2+..., and whenever I have new measurements I can use the model to make predictions.

If I on the other hand make a predictive model using random forests, I'm not really sure how to use it. I've used the caret package to split my data into training and test sets, and do automatic feature selection using random forest and cross-validation. I get good predictions on the test set, but have no idea how to implement these trees to use in say a digital signal processor. In R I just use the predict() function, but this is obviously not available outside of R.

This is probably a stupid questing, but it's the best I can do.

Any suggestions are welcome.

$\endgroup$
4
$\begingroup$

Linear regressions are great because you can implement prediction very simply in any program that can multiply and add.

Random forests, on the other hand, are much more complicated. They are made up individually of decision trees, which can basically be represented by a set of rules. However, a random forest may have hundreds or thousands of individual trees, which would be very tedious to implement by hand in another system.

Your best bet is probably going to be to find a random forest implementation for the system you wish to export the model to, and then use PMML to export the model. The RPMML package will let you convert your random forest to an XML file, which you should be able to import to any system that supports PMML.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Much appreciated! (don't have enough reputation to up-vote) $\endgroup$ – dafeda May 22 '13 at 17:39
0
$\begingroup$

You certainly have to invest some work in that, but that's not really that bad to export the randomForest model; the getTree function dumps individual trees in a very compact and nice format like this (documented in ?getTree):

> getTree(iris_rf,3)
   left daughter right daughter split var split point status prediction
1              2              3         4        0.80      1          0
2              0              0         0        0.00     -1          1
3              4              5         4        1.75      1          0
4              6              7         1        5.00      1          0
5              8              9         4        1.85      1          0
6             10             11         2        2.45      1          0
7             12             13         2        2.25      1          0
8             14             15         1        5.95      1          0
9              0              0         0        0.00     -1          3
10             0              0         0        0.00     -1          2
11             0              0         0        0.00     -1          3
12            16             17         1        6.10      1          0
13             0              0         0        0.00     -1          2
14             0              0         0        0.00     -1          2
15             0              0         0        0.00     -1          3
16            18             19         3        4.50      1          0
17             0              0         0        0.00     -1          2
18             0              0         0        0.00     -1          2
19             0              0         0        0.00     -1          3

and combining predictions of the whole ensemble is just a matter of summing up the votes (for classification) or calculating mean (for regression). I have once written a converter that was eating those outputs and generating C code (with lots of gotos) and it was working quite well.

| cite | improve this answer | |
$\endgroup$
0
$\begingroup$

I recently developed a Python package that exports C code from Random Forests classifier trained with Scikit learn: https://github.com/jonnor/emtrees

It could be used as an example of how to transform a R model to C code, by combined with the getTree shown by mbq answer.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.