前端框架选型是企业提升开发效率与用户体验的关键因素
721
2022-11-16
保存和加载scikit-learn模型
保存和加载scikit-learn模型
有时版本不一致,我们保存的时候需要附加上版本
21.1 Saving and Loading a scikit-learn Model¶ProblemYou have trained a scikit-learn model and want to save it and load it elsewhere.SolutionSave the model as a pickle file:# 保存和加载sk模型# load librariesfrom sklearn.ensemble import RandomForestClassifierfrom sklearn import datasetsfrom sklearn.externals import joblib# load datairis = datasets.load_iris()features = iris.datatarget = iris.target# create decision tree classifier objectclassifier = RandomForestClassifier()# train modelmodel = classifier.fit(features, target)# save model as pickle file 存储为pickle格式joblib.dump(model, "model.pkl")C:\ProgramData\Anaconda3\lib\site-packages\sklearn\externals\joblib\__init__.py:15: FutureWarning: sklearn.externals.joblib is deprecated in 0.21 and will be removed in 0.23. Please import this functionality directly from joblib, which can be installed with: pip install joblib. If this warning is raised when loading pickled models, you may need to re-serialize those models with scikit-learn 0.21+. warnings.warn(msg, category=FutureWarning)['model.pkl']Once the model is saved we can use scikit-learn in our destination application (e.g., web application) to load the model:# load model from file 从文件中加载模型classifier = joblib.load("model.pkl")And use it to make predictions# create new observation 样本new_observation = [[ 5.2, 3.2, 1.1, 0.1]]# predict obserrvation's class 预测classifier.predict(new_observation)array([0])DiscussionThe first step in using a model in production is to save that model as a file that can be loaded by another application or workflow. We can accomplish this by saving the model as a pickle file, a Python-specific data format. Specifically, to save the model we use joblib, which is a library extending pickle for cases when we have large NumPy arrays--a common occurance for trained models in scikit-learn.When saving scikit-learn models, be aware that saved models might not be compatible between versions of scikit-learn; therefore, it can be helpful to include the version of scikit-learn used in the model in the filename:# import library 有时版本不一致,我们保存的时候需要附加上版本import sklearn# get scikit-learn versionscikit_version = joblib.__version__# save model as pickle file 保存模型joblib.dump(model, "model_(version).pkl".format(version=scikit_version))
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~