博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
scikit-learn:3.4. Model persistence
阅读量:6638 次
发布时间:2019-06-25

本文共 2087 字,大约阅读时间需要 6 分钟。

參考: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 :

>>> 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>>> joblib.dump(clf, 'filename.pkl') >>> clf = joblib.load('filename.pkl') 

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 
.

转载地址:http://odsvo.baihongyu.com/

你可能感兴趣的文章
iOS获取设备型号和App版本号等信息(OC+Swift)
查看>>
纯CSS3鼠标滑过按钮动画过滤特效
查看>>
web端 图片上传
查看>>
17代码分离
查看>>
18Lua与C#交互
查看>>
01:UI框架加强版
查看>>
PureMVC 简单案例
查看>>
PureMVC 开发FlappyBird
查看>>
PureMVC思想
查看>>
PureMVC 开发App应用
查看>>
PureMVC
查看>>
ulua介绍和使用
查看>>
ulua介绍和使用2
查看>>
ulua热更新
查看>>
is和as
查看>>
动态类型
查看>>
EventTrigger
查看>>
C# 特性
查看>>
02.A*
查看>>
02.基础框架Mono模块
查看>>