将python随机森林模型保存到文件

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

你说的问题叫模型持久化,就是把学习好的模型保存起来,以后只要调用这个文件就可以了。
每个框架都应该有模型持久化函数,以sklearn为例:
 

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

或者: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) 

 

你可能感兴趣的:(#,Random,Forest,python,机器学习)