Use pickle to achieve model persistence

Sometimes we create a model, but when we want to use this model repeatedly, it is more complicated. We need to repeatedly write this model in different files. At this time, pickle comes in great use, and it can help us The created model is similar to the package, saved as a different .pickle file, and then when we want to call this model, just import it. For details on how to operate, see the following analysis

Save model

Using the following code will automatically generate a file named model.pickle in the current directory, wb means that writing is allowed and is in binary form

import pickle
with open("model.pickle", "wb") as f:  # 保存模型
pickle.dump(model, f)

Use model

import pickle # 导入pickle用于接下来引用我们保存的模型

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

test = [[10,16,6,22,0.62,53]] # 这里是用于预测的特征值,可以自己设置

y_predict = model.predict(test)
print(y_predict)

Guess you like

Origin blog.csdn.net/banxiaCSDN/article/details/112714525