Skip to content
Advertisement

Load Machine Learning sklearn models (RandomForestClassifier) through java and send as argument to a function in python file

I have a ML model which is trained as saved as pickle file, Randomforestclassifier.pkl. I want to load this one time using java and then execute my “prediction” part code which is written python. So my workflow is like:

  1. Read Randomforestclassifier.pkl file (one time)
  2. Send this model as input to function defined in “python_file.py” which is executed from java for each request
  3. python_file.py has prediction code and predictions returned should be captured by java code

Please provide suggestions for this workflow requirement I have used processbuilder in java to execute python_file.py and everything works fine except for model loading as one time activity

Advertisement

Answer

You could use Jep.

I actually never tested the pickle module in Jep, but for your case it would be something like this:

JavaScript

Assuming you have a module named predictionModule.py which should be something like this:

JavaScript

Hope this helps.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement