scikit-learn:3.4. Model persistence
參考:http://scikit-learn.org/stable/modules/model_persistence.html
训练了模型之后,我们希望能够保存下来,遇到新样本时直接使用已经训练好的保存了的模型。而不用又一次再训练模型。
本节介绍pickle在保存模型方面的应用。
(After
training a scikit-learn model, it is desirable to have a way to persist the model for future use without having to retrain. The following section gives you an example of how to persist a model with pickle. We’ll also review a few security and maintainability
issues when working with pickle serialization.)
1、persistence example
It
is possible to save a model in the scikit by using Python’s built-in persistence model, namely pickle:
>>> from sklearn import svm
>>> from sklearn import datasets
>>> clf = svm.SVC()
>>> iris = datasets.load_iris()
>>> X, y = iris.data, iris.target
>>> clf.fit(X, y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
kernel='rbf', max_iter=-1, probability=False, random_state=None,
shrinking=True, tol=0.001, verbose=False) >>> import pickle
>>> s = pickle.dumps(clf)
>>> clf2 = pickle.loads(s)
>>> clf2.predict(X[0])
array([0])
>>> y[0]
0
有些情况下(more
efficient on objects that carry large numpy arrays internally)使用joblib’s 取代pickle (joblib.dump & joblib.load)。之后我们甚至能够在还有一个pathon程序中load保存好的模型(pickle也能够。。。):
>>> from sklearn.externals import joblib
>>> <strong>joblib.dump(clf, 'filename.pkl')
>>> clf = joblib.load('filename.pkl') </strong>
Note
joblib.dump returns a list of filenames. Each individual numpy array contained in the clf object
is serialized as a separate file on the filesystem. All files are required in the same folder when reloading the model with joblib.load.
2、security & maintainability limitations
pickle
(and joblib by extension)在maintainability and security方面有些问题。由于:
- Never unpickle untrusted data
- Models saved in one version of scikit-learn might not load in another version.
为了可以在scikit-learn未来的版本号中重构已保存好的模型,须要pickled时加入一些metadata:
- The training data, e.g. a reference to a immutable snapshot
- The python source code used to generate the model
- The versions of scikit-learn and its dependencies
- The cross validation score obtained on the training data
further discussion,refer this talk
by Alex Gaynor.
scikit-learn:3.4. Model persistence的更多相关文章
- scikit learn 模块 调参 pipeline+girdsearch 数据举例:文档分类 (python代码)
scikit learn 模块 调参 pipeline+girdsearch 数据举例:文档分类数据集 fetch_20newsgroups #-*- coding: UTF-8 -*- import ...
- (原创)(三)机器学习笔记之Scikit Learn的线性回归模型初探
一.Scikit Learn中使用estimator三部曲 1. 构造estimator 2. 训练模型:fit 3. 利用模型进行预测:predict 二.模型评价 模型训练好后,度量模型拟合效果的 ...
- (原创)(四)机器学习笔记之Scikit Learn的Logistic回归初探
目录 5.3 使用LogisticRegressionCV进行正则化的 Logistic Regression 参数调优 一.Scikit Learn中有关logistics回归函数的介绍 1. 交叉 ...
- Scikit-learn:模型选择Model selection
http://blog.csdn.net/pipisorry/article/details/52250983 选择合适的estimator 通常机器学习最难的一部分是选择合适的estimator,不 ...
- Scikit Learn: 在python中机器学习
转自:http://my.oschina.net/u/175377/blog/84420#OSC_h2_23 Scikit Learn: 在python中机器学习 Warning 警告:有些没能理解的 ...
- 懒人小工具:自动生成Model,Insert,Select,Delete以及导出Excel的方法
在开发的过程中,我们为了节约时间,往往会将大量重复机械的代码封装,考虑代码的复用性,这样我们可以节约很多时间来做别的事情.最近跳槽到一节webform开发的公司,主要是开发自己公司用的ERP.开始因为 ...
- JS--bom对象:borswer object model浏览器对象模型
bom对象:borswer object model浏览器对象模型 navigator获取客户机的信息(浏览器的信息) navigator.appName;获得浏览器的名称 window:窗口对象 a ...
- JS--dom对象:document object model文档对象模型
dom对象:document object model文档对象模型 文档:超文本标记文档 html xml 对象:提供了属性和方法 模型:使用属性和方法操作超文本标记性文档 可以使用js里面的DOM提 ...
- 深度学习课程笔记(二)Classification: Probility Generative Model
深度学习课程笔记(二)Classification: Probility Generative Model 2017.10.05 相关材料来自:http://speech.ee.ntu.edu.tw ...
随机推荐
- Mac下Git的安装和卸载
1.安装最新版本:https://git-scm.com/download/mac,下载pkg进行安装 2.卸载:运行/usr/local/git/uninstall.sh
- 通过案例对SparkStreaming透彻理解三板斧之三
本课将从二方面阐述: 一.解密SparkStreaming Job架构和运行机制 二.解密SparkStreaming容错架构和运行机制 一切不能进行实时流处理的数据都将是无效的数据.在流处理时代,S ...
- Python 获取图片格式及像素宽高信息
# coding: utf8 from PIL import Image img = Image.open("img.jpg") print img.sizeprint img.f ...
- springboot集成mybatis-generator
首先上下成功后的效果: 配置非常简单,我们是通过maven插件来进行的,一共只需要3步: 第一步添加mysql依赖和mysql的maven插件: 由于是非常简单的spring+mysql的原始项目,我 ...
- DevExpress.Build
using System.Collections.Generic; using Microsoft.Build.AppxPackage; using Microsoft.Build.Framework ...
- 在redhat下使用x11vnc进行桌面共享
1.在redhat上安装x11vnc时.你须要注意下面几个方面: (1)下载x11vnc的源代码包: 网址例如以下所看到的: http://sourceforge.net/projects/libvn ...
- python abstractmethod 对象比较
from functools import total_ordering from abc import ABCMeta,abstractmethod @total_ordering class Sh ...
- lombok使用总结
前提 这篇文章主要介绍lombok的使用,至于lombok的源码和原理暂不探究,可以看上一篇文章插件化注解处理API去了解lombok的基本原理.参考资料: lombok官网 lombok官方教程-l ...
- LoadRunner测试WebService的3种方式
LR在WebService虚拟用户协议中支持两种方式测试WebService,一种是通过“Add Service Call”的方式,一种是Import SOAP的方式. Import SOAP的方式需 ...
- 【Oracle】查找每期数据都存在的产品
现在存在以下数据 如上图:A01与A02同时存在201710.201711.201712中 我们现在要将其查找出来 如果上图的表结构如下: 那么查询的SQL如下: SELECT DISTINCT CO ...