在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 of 2 which can be read by Python 2.

    Check the protocolparameter in pickle.dump. Your resulting code will look like this.

    pickle.dump(your_object, your_file, protocol=2)

    There is no protocolparameter in pickle.load because pickle can determine the protocol from the file.

    Pickle uses different protocols to convert your data to a binary stream.

    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 the protocol parameter when invoking pickle.dump.

     

python 2/3 joblib.dump() 和 joblib.load()的更多相关文章

  1. python中json文件处理涉及的四个函数json.dumps()和json.loads()、json.dump()和json.load()的区分

    一.概念理解 1.json.dumps()和json.loads()是json格式处理函数(可以这么理解,json是字符串) (1)json.dumps()函数是将一个Python数据类型列表进行js ...

  2. 【Python基础】json.dumps()和json.loads()、json.dump()和json.load()的区分

    json文件处理涉及的四个函数json.dumps()和json.loads().json.dump()和json.load()的区分 一.概念理解 1.json.dumps()和json.loads ...

  3. python 读写json文件(dump, load),以及对json格式的数据处理(dumps, loads)

    JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. 1.json.dumps()和json.loads()是json ...

  4. python---json.dumps 与 json.loads /json.dump 和json.load区别

    json.dumps 是将python的数据类型进行json的编码,生成json格式的数据,举例json_data = json.dumps(str)  str为python的字符串类型数据,生成的j ...

  5. json.dump()和json.load()

    import json,time # save data to json file def store(data): with open('data.json', 'w') as fw: # 将字典转 ...

  6. 终于解决了python 3.x import cv2 “ImportError: DLL load failed: 找不到指定的模块” 及“pycharm关于cv2没有代码提示”的问题

    终于解决了python 3.x import cv2 “ImportError: DLL load failed: 找不到指定的模块” 及“pycharm关于cv2没有代码提示”的问题   参考 :h ...

  7. json模块:json.dumps()、json.loads()、json.dump()、json.load()

    json.dumps().json.loads().json.dump().json.load() 4个方法的总结和使用: 注意:存在文件里面的东西,读出来都是字符串 import json 1.js ...

  8. Python中Pickle模块的dump()方法和load()方法

    Python中的Pickle模块实现了基本的数据序列与反序列化. 经常遇到在Python程序运行中得到了一些字符串.列表.字典等数据,想要长久的保存下来,方便以后使用,而不是简单的放入内存中关机断电就 ...

  9. python采用json.dump和json.load存储数据

    #!/usr/bin/python # -*- coding: UTF-8 -*- import json numbers = [2,3,4,7,11,13] filename = 'numbers. ...

随机推荐

  1. ELM327 蓝牙/WIFI/USB diagnostic interface

  2. Spring Bean init-method 和 destroy-method实例

    在Spring中,可以使用 init-method 和 destroy-method 在bean 配置文件属性用于在bean初始化和销毁某些动作时.这是用来替代 InitializingBean和Di ...

  3. MySql清空表的方法介绍 : truncate table 表名

    清空某个mysql表中所有内容 delete from 表名; truncate table 表名; 不带where参数的delete语句可以删除mysql表中所有内容,使用truncate tabl ...

  4. 实施CMMI3的体会

    公司从去年年底开始实施CMMI3,记得当初上培训课的时候,听着老师介绍过程管理,项目管理,工程过程,支持过程这四大类过程域的时候,全部门上下听得稀里糊涂,从未想到这个鬼东西还这么复杂,这么麻烦. 公司 ...

  5. ffmpeg的IO操作

    ffmpeg 可以通过IO操作将数据读取和存储在文件或网络中 作为数据的读取和写入地址,数据被存放在file,http, ffmpeg 不仅可以编解常用的音视频格式,还可以将数据导入/导出到各种媒介中 ...

  6. PHP抓取页面中的邮箱

    <?php $url='http://www.cnblogs.com/tinyphp/p/3234926.html'; //当页已留邮箱 $content=file_get_contents($ ...

  7. Silverlight:《Pro Silverlight5》读书笔记 之 Dependency Properties And Routed Event

    Dependency Properties And Routed Event Dependency Properties Dynamic Value Resolution As you’ve alre ...

  8. (原创)2. WPF中的依赖属性之二

    1 依赖属性 1.1 依赖属性最终值的选用 WPF属性系统对依赖属性操作的基本步骤如下: 第一,确定Base Value,对同一个属性的赋值可能发生在很多地方.还用Button的宽度来进行举例,可能在 ...

  9. STL源码剖析——hashtable

    二叉搜索树具有对数时间的搜索复杂度,但是这样的复杂度是再输入数据有足够的随机性的假设上哈希表在插入删除搜索操作上也具有常数时间的表现,而且这种表现是以统计为基础,不需要依赖输入元素的随机性 hasht ...

  10. 探寻C++最快的读取文件的方案 ——C++ IO优化

    在竞赛中,遇到大数据时,往往读文件成了程序运行速度的瓶颈,需要更快的读取方式.相信几乎所有的C++学习者都在cin机器缓慢的速度上栽过跟头,于是从此以后发誓不用cin读数据.还有人说Pascal的re ...