Save python random forest model to file

Viewed 34412

In R, after running "random forest" model, I can use save.image("***.RData") to store the model. Afterwards, I can just load the model to do predictions directly.

Can you do a similar thing in python? I separate the Model and Prediction into two files. And in Model file:

rf= RandomForestRegressor(n_estimators=250, max_features=9,compute_importances=True)
fit= rf.fit(Predx, Predy)

I tried to return rf or fit, but still can't load the model in the prediction file.

Can you separate the model and prediction using the sklearn random forest package?

5 Answers

You can use joblib to save and load the Random Forest from scikit-learn (in fact, any model from scikit-learn)

The example:

import joblib
from sklearn.ensemble import RandomForestClassifier
# create RF
rf = RandomForestClassifier()
# fit on some data
rf.fit(X, y)

# save
joblib.dump(rf, "my_random_forest.joblib")

# load
loaded_rf = joblib.load("my_random_forest.joblib")

What is more, the joblib.dump has compress argument, so the model can be compressed. I made very simple test on iris dataset and compress=3 reduces the size of the file about 5.6 times.

I'd reiterate that joblib does the job well and it provides really good compression options (ie lzma).

with open("clf.pkl", "wb") as out: pickle.dump(clf, out)
with open("clf.dill", "wb") as out: dill.dump(clf, out)
joblib.dump(clf, "clf.jbl")
joblib.dump(clf, "clf.jbl.lzma")
joblib.dump(clf, "clf.jbl.gz")

!du clf.*
24576   clf.dill
24576   clf.jbl
5120    clf.jbl.gz
3072    clf.jbl.lzma
24576   clf.pkl
Related