Explore Courses Blog Tutorials Interview Questions
0 votes
in Machine Learning by (19k points)

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=, 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?

1 Answer

0 votes
by (33.1k points)

Simply use python cpickle library for this:

import cPickle

rf = RandomForestRegresor(), y)

with open('path/to/file', 'wb') as f:

    cPickle.dump(rf, f)

# in your prediction file                                                                                                                           

with open('path/to/file', 'rb') as f:

    rf = cPickle.load(f)

preds = rf.predict(new_X)

Hope this answer helps. For more details in this, study Random Forest In Python.

Browse Categories