1.使用模块joblib
    使用joblib模块更加的简单了,核心代码就两行

    pip install joblib

    1. from sklearn.ensemble import RandomForestClassifier
    2. from sklearn import datasets
    3. from joblib import joblib
    4. (X,y) = datasets.load_iris(return_X_y=True)
    5. model = RandomForestClassifier(n_estimators=100,max_depth=100)
    6. model.fit(X,y)
    7. print(model.predict(X[0:1,:]))
    8. #save model
    9. joblib.dump(modle, 'saved_model/rfc.pkl')
    10. #load model
    11. model2= joblib.load('saved_model/rfc.pkl')
    12. print(model2.predict(X[0:1,:]))