I would like to train a Random Forest model (RandomForestRegressor pkg=ScikitLearn) and save the trained model to disk. Then, I would like to load the model and apply it to new data.
After training with success, I tried to save the model using JLD2. Once I read the model and apply it again I get the error
ERROR: LoadError: ArgumentError: ref of NULL PyObject
Here is my code:
using MLJ
using ScikitLearn
using MLJScikitLearnInterface
using PyCall
using JLD2
x = rand(Float32, 100, 10) # 100 training samples, 10 predictors
y = x[:,2] + x[:,4]
@MLJ.load RandomForestRegressor pkg=ScikitLearn
clf = RandomForestRegressor()
# Train the model
mach = machine(clf, x, y)
MLJ.fit!(mach, verbosity=2)
yTR_hat = MLJ.predict(mach, x)
# Save the model
@JLD2.save "model.jld2" mach
# Load the saved model and apply to new data
@JLD2.load "model.jld2" mach
# This gives me the ERROR: ArgumentError: ref of NULL PyObject
yTE_hat = MLJ.predict(mach, x)
Please, how should I do this?
Thank you very much for the help