1.使用模块joblib
使用joblib模块更加的简单了,核心代码就两行
pip install joblib
from sklearn.ensemble import RandomForestClassifier
from sklearn import datasets
from joblib import joblib
(X,y) = datasets.load_iris(return_X_y=True)
model = RandomForestClassifier(n_estimators=100,max_depth=100)
model.fit(X,y)
print(model.predict(X[0:1,:]))
#save model
joblib.dump(modle, 'saved_model/rfc.pkl')
#load model
model2= joblib.load('saved_model/rfc.pkl')
print(model2.predict(X[0:1,:]))