Save python random forest model to file

https://zhidao.baidu.com/question/1707465980177376940.html

The problem you mentioned is model persistence, which is to save the learned model, and then just call this file in the future.
Every framework should have a model persistence function. Take sklearn as an example:
 

from sklearn.externals import joblib
joblib.dump(clf, "train_model.m") #存储
clf = joblib.load("train_model.m") #调用

Or: http://cn.voidcc.com/question/p-ntavhtii-bag.html

... 
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) 

 

Guess you like

Origin blog.csdn.net/ch206265/article/details/108971094