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 ...
随机推荐
- JS向后台传递json数组对象
var Obj = []; //一下代码可以循环插入 var returnObj = new Object();//创建一个对象 returnObj.id = “123”: returnObj.mon ...
- easyui datagrid checkbox的相关属性整理
DataGrid其中与选择,勾选相关 DataGrid属性: singleSelect boolean 如果为true,则只允许选择一行. false ctrlSelect boolean 在启用多行 ...
- J2EE开发工作中遇到的异常问题及解决方法总结
参考博文:http://blog.csdn.net/rchm8519/article/details/41624381
- IDEA Maven 下载jar
转载地址:https://www.cnblogs.com/hongwz/p/5456578.html 本文主要介绍通过配置Maven来下载需要用的jar包. 比如我现在用到把字符串转成json对象,这 ...
- Java调用Oracle存储过程
package com.hp.test; import java.sql.CallableStatement; import java.sql.Connection; import java.sql. ...
- CocoSourcesCS 4
/*------------------------------------------------------------------------- ParserGen.cs -- Generati ...
- [Typescript] Make TypeScript Class Usage Safer with Strict Property Initialization
By setting the strictPropertyInitialization flag in the .tsconfig file, TypeScript will start throwi ...
- ssh中使用spring的集成quartz 编写定时任务
之前没有使用框架开发时对于开发定时任务都是 使用java的原声timer类,重写线程的run方法跑要执行的任务.刚刚换的新公司,项目使用ssh2,目前该项目中的定时任务的使用spirng集成的quar ...
- sql中limit和汇总函数的集合使用
limit和求和一起 select SUM(A.high) from (select * from minute1_K order by DateTime desc limit 10) A limit ...
- libevent2源码分析之四:libevent2的消息泵
Dispatch类似于一个消息泵,在一个死循环中,不停地检查IO的状态(可以想像成不断从消息队列中读取消息),将状态的改变变成事件,再进行事件的响应. 主要代码如下: [event.c] int ev ...