Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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= 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?

1 Answer

0 votes
by (33.1k points)

Simply use python cpickle library for this:

import cPickle

rf = RandomForestRegresor()

rf.fit(X, 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.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...