赞
踩
from sklearn.ensemble import RandomForestClassifier from sklearn import datasets import pickle #方法一:python自带的pickle (X,y) = datasets.load_iris(return_X_y=True) rfc = RandomForestClassifier(n_estimators=100,max_depth=100) rfc.fit(X,y) print(rfc.predict(X[0:1,:])) #save model f = open('saved_model/rfc.pickle','wb') pickle.dump(rfc,f) f.close() #load model f = open('saved_model/rfc.pickle','rb') rfc1 = pickle.load(f) f.close() print(rfc1.predict(X[0:1,:]))
使用joblib模块更加的简单了,核心代码就两行
from sklearn.ensemble import RandomForestClassifier
from sklearn import datasets
from sklearn.externals import joblib
#方法二:使用sklearn中的模块joblib
(X,y) = datasets.load_iris(return_X_y=True)
rfc = RandomForestClassifier(n_estimators=100,max_depth=100)
rfc.fit(X,y)
print(rfc.predict(X[0:1,:]))
#save model
joblib.dump(rfc, 'saved_model/rfc.pkl')
#load model
rfc2 = joblib.load('saved_model/rfc.pkl')
print(rfc2.predict(X[0:1,:]))
#save model
joblib.dump(rfc, 'saved_model.pkl')
#load model
rfc2 = joblib.load('saved_model.pkl')
rfc2继续训练预测
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。