简体   繁体   English

如何使用pickle保存sklearn模型

[英]How to use the pickle to save sklearn model

I want to dump and load my Sklearn trained model using Pickle.我想使用 Pickle 转储和加载我的 Sklearn 训练模型。 How to do that?怎么做?

Save:节省:

with open("model.pkl", "wb") as f:
    pickle.dump(model, f)

Load:加载:

with open("model.pkl", "rb") as f:
    model = pickle.load(f)

Using pickle is same across all machine learning models irrespective of type ie clustering, regression etc.无论类型如何,如聚类、回归等,在所有机器学习模型中使用 pickle 都是相同的。

To save your model in dump is used where 'wb' means write binary.将您的模型保存在转储中,其中“wb”表示写入二进制文件。

pickle.dump(model, open(filename, 'wb')) #Saving the model

To load the saved model wherever need load is used where 'rb' means read binary.在需要加载的地方加载保存的模型,其中“rb”表示读取二进制文件。

model = pickle.load(open(filename, 'rb')) #To load saved model from local directory

Here model is kmeans and filename is any local file, so use accordingly.这里模型是kmeans,文件名是任何本地文件,因此请相应地使用。

One can also use joblib也可以使用joblib

from joblib import dump, load
dump(model, model_save_path) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM