Python scikit-learn:导出训练好的分类器 [英] Python scikit-learn: exporting trained classifier

查看:74
本文介绍了Python scikit-learn:导出训练好的分类器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用基于 scikit-learn 的来自 nolearn 的 DBN(深度信念网络).

我已经建立了一个可以很好地对我的数据进行分类的网络,现在我对导出模型进行部署感兴趣,但我不知道如何(每次我想预测某事时我都在训练 DBN).在 matlab 中,我只需导出权重矩阵并将其导入另一台机器.

有人知道如何导出模型/要导入的权重矩阵而无需再次训练整个模型吗?

解决方案

首先,安装joblib.

您可以使用:

<预><代码>>>>导入作业库>>>joblib.dump(clf, 'my_model.pkl', compress=9)

然后,在预测服务器上:

<预><代码>>>>导入作业库>>>model_clone = joblib.load('my_model.pkl')

这基本上是一个 Python pickle,对大型 numpy 数组进行了优化处理.它与常规泡菜 w.r.t. 具有相同的限制.代码更改:如果 pickle 对象的类结构发生变化,您可能无法再使用新版本的 nolearn 或 scikit-learn 解开该对象.

如果您希望以长期稳健的方式存储模型参数,您可能需要编写自己的 IO 层(例如使用二进制格式序列化工具,如协议缓冲区或 avro 或低效但可移植的文本/json/xml 表示,例如如 PMML).

I am using a DBN (deep belief network) from nolearn based on scikit-learn.

I have already built a Network which can classify my data very well, now I am interested in exporting the model for deployment, but I don't know how (I am training the DBN every time I want to predict something). In matlab I would just export the weight matrix and import it in another machine.

Does someone know how to export the model/the weight matrix to be imported without needing to train the whole model again?

解决方案

First, install joblib.

You can use:

>>> import joblib
>>> joblib.dump(clf, 'my_model.pkl', compress=9)

And then later, on the prediction server:

>>> import joblib
>>> model_clone = joblib.load('my_model.pkl')

This is basically a Python pickle with an optimized handling for large numpy arrays. It has the same limitations as the regular pickle w.r.t. code change: if the class structure of the pickle object changes you might no longer be able to unpickle the object with new versions of nolearn or scikit-learn.

If you want long-term robust way of storing your model parameters you might need to write your own IO layer (e.g. using binary format serialization tools such as protocol buffers or avro or an inefficient yet portable text / json / xml representation such as PMML).

这篇关于Python scikit-learn:导出训练好的分类器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆