0

I have been working on an algorithm trading project where I used R to fit a random forest using historical data while the real-time trading system is in Python.

I have fitted a model I'd like to use in R and am now wondering how can I use this model for prediction purposes in the Python system.

Thanks.

DanielZuo
  • 33
  • 5
  • you could export results from R to a text file and read them in with python. – kpie Feb 12 '20 at 14:36
  • 1
    An alternative is to use reticulate in R to run your python program. Otherwise you could see what R is saving your model as and simply check how to import it into Python. https://machinelearningmastery.com/save-load-machine-learning-models-python-scikit-learn/ is probably another point to start – Jason Chia Feb 12 '20 at 14:38
  • @kpie thanks. The python program is for real-time trading purpose instead of data analytics purpose. Hence python only needs to fitted model to predict a value based on real-time inputs. – DanielZuo Feb 12 '20 at 14:40
  • @JasonChia Thanks for the pointers. I think reticulate could potentially work as I can just pass the real-time data to R for it to spit out an output. So the prediction is actually done in R and pass back to python, if I understand correctly? – DanielZuo Feb 12 '20 at 14:42
  • @DanielZuo Yeah. Something like that. R program runs continuously. R-> gets "real-time" data from python script. R predicts (actions a or b) -> launches script which does the actions. ALthough I do agree with Nikhil Gupta in setting up your training and model natively in Python. Much more efficient. – Jason Chia Feb 12 '20 at 15:10

1 Answers1

1

There are several options:

(1) Random Forest is a well researched algorithm and is available in Python through sci-kit learn. Consider implementing it natively in Python if that is the end goal.

(2) If that is not an option, you can call R from within Python using the Rpy2 library. There is plenty of online help available for this library, so just do a google search for it.

Hope this helps.

Nikhil Gupta
  • 1,436
  • 12
  • 15