0
$\begingroup$

I have implemented random forest with R's Random Forest package. The forest has 500 trees and 12 predictors. What should be the running time of function Predict? it takes around 57 millisecond. is there faster implementation?

test time to predict:

options(digits.secs=6)
start = Sys.time()
for (i in 1:1000) {
   predict(rf,test[1,])
}
end = Sys.time()

Thanks

$\endgroup$
1
  • $\begingroup$ predict.randomForest is only a wrapper of the internal C-code. You may cut a lot of overhead writing a custom interface in C. $\endgroup$ Sep 10, 2015 at 7:44

1 Answer 1

1
$\begingroup$

The wall clock time for predict.randomForest() is dependent on the number of trees and predictors, as you've specified. However, it's also going to depend on the hardware you're using, as well as any other processing going on at the same time. I'm not sure whether anyone can determine how long it should take in your particular environment. Do you have a threshhold <57ms that you're trying to hit?

One brute force way to speed things up would be to run your predictions in parallel. That is, instead of running 1 prediction 1000 times, run 1000 predictions in parallel just once. Of course that's extreme, so work out how many cores/processors you can spare at once, and chunk up your 1000 predictions across those cores. Be aware that if your dataset is small, the overhead in coordination of the cores/data might offset any savings from the parallelization.

For example, on my 4 core machine, I want one core unused. So I'd use the doParallel parallel backend (I'm running Windows) and the foreach package to register 3 cores and run 333 loops on 2 cores and 334 on the third core. The same process is analagous to if you want to score 1000 observations from your test dataset just once.

A good reference for the outlined suggestion can be found at https://cran.r-project.org/web/packages/doParallel/vignettes/gettingstartedParallel.pdf And similarly, using the doSNOW parallel backend, the author provides runtime benchmarks at http://www.vikparuchuri.com/blog/parallel-r-model-prediction-building/ A somewhat related parallel prediction post from SO has a few other suggestions on dataset splitting that I don't have personal experience with https://stackoverflow.com/questions/28695076/parallel-predict

$\endgroup$
4
  • $\begingroup$ 1. I am using the predict.randomForest() for real-time software, so I have just one prediction at a given time - can't run in parallel. 2. is Predict calculate the votes (of trees in the forest) in parallel? 3. On 2 CPU (8 cores) I got 33 ms, and when I'm using Jpmml (R to java) I got 8 ms on average for evaluate() (same as predict). 4. I'm trying to get to 1 millisecond for predict(). any other suggestions? Thanks, $\endgroup$
    – Adir Cohen
    Sep 9, 2015 at 11:21
  • $\begingroup$ I can't find any indications in the docuemntation that it does; and I don't happen to have an R instance in front of me where I can double check the source code. If you have time to hack about, you might try the ranger package, which markets itself as "a fast implementation of Random Forests". The predict function has a threads= parameter which leads me to think it might parallelize the trees across multiple simultaneous cores. cran.r-project.org/web/packages/ranger/index.html $\endgroup$
    – Amw 5G
    Sep 9, 2015 at 11:23
  • $\begingroup$ you can train 2, 4 or 8 sub-forests with less trees and predict these in parallel. In the end you may want to consider a pure Java or C++ implementation. $\endgroup$ Sep 10, 2015 at 8:44
  • $\begingroup$ I've implemented it in Java (Weka Library) and got about 0.7 ms for a classification (Java much faster than R, same hardware). unfortunately, I'm getting poor results relative to R (although it is the same data and configuration - same number of trees and mtry). any suggestions? $\endgroup$
    – Adir Cohen
    Sep 11, 2015 at 8:02

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.