python 2/3 joblib.dump() 和 joblib.load()
在python2中加载python3训练和保存的模型时出错: ValueErrorTraceback (most recent call last) --> 237 clf = joblib.load('clf300_all.model') 238 pred_y = clf.predict_proba(X) /usr/local/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle.pyc in load(filename, mmap_mode) 576 return load_compatibility(fobj) 577 --> 578 obj = _unpickle(fobj, filename, mmap_mode) 579 580 return obj /usr/local/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle.pyc in _unpickle(fobj, filename, mmap_mode) ValueError: unsupported pickle protocol: 3
经过查阅资料:
跨python版本的 joblib.dump() 和 joblib.load()
Compatibility across python versions
Compatibility of joblib pickles across python versions is not fully supported. Note that, for a very restricted set of objects, this may appear to work when saving a pickle with python 2 and loading it with python 3 but relying on it is strongly discouraged.
If you are switching between python versions, you will need to save a different joblib pickle for each python version.
Here are a few examples or exceptions:
Saving joblib pickle with python 2, trying to load it with python 3:
Traceback (most recent call last): File "/home/lesteve/dev/joblib/joblib/numpy_pickle.py", line 453, in load obj = unpickler.load() File "/home/lesteve/miniconda3/lib/python3.4/pickle.py", line 1038, in load dispatch[key[0]](self) File "/home/lesteve/miniconda3/lib/python3.4/pickle.py", line 1176, in load_binstring self.append(self._decode_string(data)) File "/home/lesteve/miniconda3/lib/python3.4/pickle.py", line 1158, in _decode_string return value.decode(self.encoding, self.errors) UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 1024: ordinal not in range(128) Traceback (most recent call last): File "<string>", line 1, in <module> File "/home/lesteve/dev/joblib/joblib/numpy_pickle.py", line 462, in load raise new_exc ValueError: You may be trying to read with python 3 a joblib pickle generated with python 2. This is not feature supported by joblib.
Saving joblib pickle with python 3, trying to load it with python 2:
Traceback (most recent call last): File "<string>", line 1, in <module> File "joblib/numpy_pickle.py", line 453, in load obj = unpickler.load() File "/home/lesteve/miniconda3/envs/py27/lib/python2.7/pickle.py", line 858, in load dispatch[key](self) File "/home/lesteve/miniconda3/envs/py27/lib/python2.7/pickle.py", line 886, in load_proto raise ValueError, "unsupported pickle protocol: %d" % proto ValueError: unsupported pickle protocol: 3
=================================================================================================================================================不完全支持跨python版本的joblib pickle的兼容性。请注意,对于一组非常有限的对象,当使用python 2保存pickle并使用python 3加载它时,这可能会起作用,但强烈建议不要依赖它。 如果要在python版本之间切换,则需要为每个python版本保存不同的joblib pickle。
==================================================================================================================================================
另外:不同python版本的pickle.dump()和pickle.load()是可以相互转换和支持的You should write the pickled data with a lower protocol number in Python 3. Python 3 introduced a new protocol with the number
3
(and uses it as default), so switch back to a value of2
which can be read by Python 2.Check the
protocol
parameter inpickle.dump
. Your resulting code will look like this.pickle.dump(your_object, your_file, protocol=2)
There is no
protocol
parameter inpickle.load
becausepickle
can determine the protocol from the file.
Pickle uses different
protocols
to convert your data to a binary stream.
In python 2 there are 3 different protocols (
0
,1
,2
) and the default is0
.In python 3 there are 5 different protocols (
0
,1
,2
,3
,4
) and the default is3
.You must specify in python 3 a protocol lower than
3
in order to be able to load the data in python 2. You can specify theprotocol
parameter when invokingpickle.dump
.
相关文章
- Python之单例模式的多种实现
- 【Python】python 日期操作
- python函数式编程
- 理解Python的双下划线命名(转)
- Python 日期和时间_python 当前日期时间_python日期格式化
- 【Python实战】python中含有中文字符无法运行
- 【Machine Learning】Python开发工具:Anaconda+Sublime
- python-操作excel数据文件
- python操作docker SDK:Docker SDK for Python
- Python编程语言学习:基于python各种库(matplotlib、Image)利用多种方法展示图片或进行图片可视化之详细攻略
- Python语言学习:在python中,如何获取变量的本身字符串名字而非其值/内容及其应用(在代码中如何查找同值的所有变量名)
- Python编程语言学习:python编程语言中重要函数讲解之map函数等简介、使用方法之详细攻略
- Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(Scale/Scale的Command)
- Python语言学习:基于python五种方法实现使用某函数名【func_01】的字符串格式('func_01')来调用该函数【func_01】执行功能
- 已解决2. Set PROTOCOL_BUPFERS_PYTHON_iMPLEMENTATION=python (but this will use pure-Python parsing and w
- 到底该怎么学python啊?
- Python MySQL选择
- 拿来即用,这5个 Python 自动化脚本好用到起飞
- Python Flask框架学习13:Flask Cookies
- Python编程:shelve模块-持久化python数据
- Python编程:查看python语法中的关键字keyword
- Python 23种设计模式全(python例子)
- 华为10年经验测试工程师,整理出来的python自动化测试实战
- 多版本Python共存时pip给指定版本的python安装package的方法
- 【异常】前端ERR! stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.
- python下openVINO使用遇到from .ie_api import * ImportError: DLL load failed的终极解决方案
- 【Python】1.python 删除文件夹和文件
- 我要偷偷的学Python,然后惊呆所有人(第九天)