在使用 joblib 加载模型之前检查 sklearn 版本 [英] Check sklearn version before loading model using joblib

查看:77
本文介绍了在使用 joblib 加载模型之前检查 sklearn 版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照本指南保存机器学习模型以备后用.该模型被转储到一台机器中:

I've followed this guide to save a machine learning model for later use. The model was dumped in one machine:

from sklearn.externals import joblib
joblib.dump(clf, 'model.pkl')

当我在另一台机器上加载它 joblib.load('model.pkl') 时,我收到了这个警告:

And when I loaded it joblib.load('model.pkl') in another machine, I got this warning:

UserWarning: 试图从 estimator DecisionTreeClassifier 中解压使用 0.18.1 版时,0.18 之前的版本.这可能会导致破坏代码或无效结果.使用风险自负.

UserWarning: Trying to unpickle estimator DecisionTreeClassifier from version pre-0.18 when using version 0.18.1. This might lead to breaking code or invalid results. Use at your own risk.

那么有什么办法可以知道保存模型的sklearn版本与当前版本进行比较吗?

So is there any way to know the sklearn version of the saved model to compare it with the current version?

推荐答案

pickled estimators 的版本控制 在 scikit-learn 0.18 中添加.从 v0.18 开始,您可以获得用于创建估算器的 scikit-learn 版本,

Versioning of pickled estimators was added in scikit-learn 0.18. Starting from v0.18, you can get the version of scikit-learn used to create the estimator with,

estimator.__getstate__()['_sklearn_version']

您收到的警告是由 估计器的 __setstate__ 方法,在 unpickling 时自动调用.看起来没有一种直接的方法可以在不从磁盘加载估算器的情况下获取此版本.你可以过滤掉警告,

The warning you get is produced by the __setstate__ method of the estimator which is automatically called upon unpickling. It doesn't look like there is a straightforward way of getting this version without loading the estimator from disk. You can filter out the warning, with,

import warnings

with warnings.catch_warnings():
      warnings.simplefilter("ignore", category=UserWarning)
      estimator = joblib.load('model.pkl')

对于 0.18 之前的版本,没有这样的机制,但我想你可以,例如,使用 not hasattr(estimator, '__getstate') 作为测试来检测,至少, pre-0.18 版本.

For pre-0.18 versions, there is no such mechanism, but I imagine you could, for instance, use not hasattr(estimator, '__getstate') as a test to detect to, at least, pre-0.18 versions.

这篇关于在使用 joblib 加载模型之前检查 sklearn 版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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