參考: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的更多相关文章

  1. scikit learn 模块 调参 pipeline+girdsearch 数据举例:文档分类 (python代码)

    scikit learn 模块 调参 pipeline+girdsearch 数据举例:文档分类数据集 fetch_20newsgroups #-*- coding: UTF-8 -*- import ...

  2. (原创)(三)机器学习笔记之Scikit Learn的线性回归模型初探

    一.Scikit Learn中使用estimator三部曲 1. 构造estimator 2. 训练模型:fit 3. 利用模型进行预测:predict 二.模型评价 模型训练好后,度量模型拟合效果的 ...

  3. (原创)(四)机器学习笔记之Scikit Learn的Logistic回归初探

    目录 5.3 使用LogisticRegressionCV进行正则化的 Logistic Regression 参数调优 一.Scikit Learn中有关logistics回归函数的介绍 1. 交叉 ...

  4. Scikit-learn:模型选择Model selection

    http://blog.csdn.net/pipisorry/article/details/52250983 选择合适的estimator 通常机器学习最难的一部分是选择合适的estimator,不 ...

  5. Scikit Learn: 在python中机器学习

    转自:http://my.oschina.net/u/175377/blog/84420#OSC_h2_23 Scikit Learn: 在python中机器学习 Warning 警告:有些没能理解的 ...

  6. 懒人小工具:自动生成Model,Insert,Select,Delete以及导出Excel的方法

    在开发的过程中,我们为了节约时间,往往会将大量重复机械的代码封装,考虑代码的复用性,这样我们可以节约很多时间来做别的事情.最近跳槽到一节webform开发的公司,主要是开发自己公司用的ERP.开始因为 ...

  7. JS--bom对象:borswer object model浏览器对象模型

    bom对象:borswer object model浏览器对象模型 navigator获取客户机的信息(浏览器的信息) navigator.appName;获得浏览器的名称 window:窗口对象 a ...

  8. JS--dom对象:document object model文档对象模型

    dom对象:document object model文档对象模型 文档:超文本标记文档 html xml 对象:提供了属性和方法 模型:使用属性和方法操作超文本标记性文档 可以使用js里面的DOM提 ...

  9. 深度学习课程笔记(二)Classification: Probility Generative Model

    深度学习课程笔记(二)Classification: Probility Generative Model  2017.10.05 相关材料来自:http://speech.ee.ntu.edu.tw ...

随机推荐

  1. angular get/post 下载 excel

    阅读目录 get请求 post请求 最近做项目,就碰到一个导出excel表格的功能.原本是想利用web前台导出excel的,但是最后因为两点放弃了,第一点,因为中文乱码,第二点,有分页(在前台导出ex ...

  2. spring注入之使用标签 @Autowired @Qualifier

      使用标签的缺点在于必需要有源代码(由于标签必须放在源代码上),当我们并没有程序源代码的时候.我们仅仅有使用xml进行配置. 比如我们在xml中配置某个类的属性            <bea ...

  3. ffmpeg 复用

    aa 将mkv中的音视频复用成ts流: ffmpeg -i 32_mkv_h264_718x480_ac3.mkv  -codec copy -bsf:v h264_mp4toannexb  -f m ...

  4. 开启GodMode

    上帝模式的开启方法:首先你可以在任何地方创建一个新文件夹,这个操作对于几乎所有电脑用户来说都非常简单,然后重要的是,将这个新文件夹重命名为 “GodMode.{ED7BA470-8E54-465E-8 ...

  5. static_cast、dynamic_cast、const_cast和reinterpret_cast总结(转)

    前言 这篇文章总结的是C++中的类型转换,这些小的知识点,有的时候,自己不是很注意,但是在实际开发中确实经常使用的.俗话说的好,不懂自己写的代码的程序员,不是好的程序员:如果一个程序员对于自己写的代码 ...

  6. (二)EasyUI 使用——常用组件

    1. EasyUI常用组件的基本用法 1.1 layout布局 <!-- 布局面板 大小自适应父容器 --> <div data-options="fit:true&quo ...

  7. MongoDB笔记(一):MongoDB介绍及Windows下安装

    一.前言 MongoDB火了也蛮久了,关于简介看看这里吧.项目中一直没用上,最近闲的慌就自己学了下,顺便记录下以便今后复习. 本系列是基于MongoDB 2.4.8 windows 64位讲解,后面的 ...

  8. c++ builder xe2 字符串转日期

    TFormatSettings * fmt = new TFormatSettings; fmt->ShortDateFormat = L"yy-mm-dd"; fmt-&g ...

  9. 解决window10系统电脑插入耳机之后没有声音的问题

    其实办法也是从百度百科上查到的 ⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄     可能是因为自己某个不小心的操作更改了设置 1. 首先要点开设置按钮,在搜索栏输入控制面板 (当然知道控制面板在哪里的小伙伴就不用 ...

  10. 什么是SAAS模式网站?

    说到“SAAS”,它的读法非常有趣,有“萨斯”,有“S.A.A.S”, 还有中文白话“啥事”的.不过,大多不熟悉的朋友第一反应可能是非典?,别误会,此“SAAS”非彼“SARS”,一字之差,但是意义完 ...