之前用 R 语言一直感觉 .Rdata 格式的文件很好用,可以把每次执行的中间文件保存便于下次调用,刚熟悉 Python 还没接触这块知识,所以有时候做项目不太顺手,索性上网搜了下,整理如下:
模型存档
############# # joblib 库 from sklearn.linear_model import LogisticRegression from sklearn.externals import joblib # 模型保存 lr_model = LogisticRegression() joblib.dump(lr_model, ‘xx.model‘) # 模型载入 lr_model = joblib.load(‘xx.model‘) ############# # pickle 库 # 模型保存 import pickle with open(‘lr_model.pickle‘, ‘wb‘) as fp: pickle.dump(lr_model, fp) # 模型载入 with open(‘lr_model.pickle‘, ‘rb‘) as fp: lr_model = pickle.load(fp)
对象保存
############# # pickle 库 import pickle x, y = 1, range(10) with open(‘xx.pickle‘, ‘wb‘) as fp: # 通过传递protocol = -1到dump()来减少文件大小 pickle.dump([x, y], fp) # 对象载入 with open(‘xx.pickle‘, ‘rb‘) as fp: x, y = pickle.load(fp) print(x) ############# # _pickle 库 # 对象保存 import _pickle as cpickle x, y = 1, range(10) with open(‘xx.pickle‘, ‘wb‘) as fp: # 通过传递protocol = -1到dump()来减少文件大小 cpickle.dump([x, y], fp) # 对象载入 del x, y with open(‘xx.pickle‘, ‘rb‘) as fp: x, y = cpickle.load(fp) print(x) ############ # dill 库 import dill # 文件保存 filename = ‘globalsave.pkl‘ dill.dump_session(filename) # 文件载入 dill.load_session(filename) ############ # 其他库如:pmml,shelve
原文:https://www.cnblogs.com/iupoint/p/10522117.html