简体   繁体   English

如何在 Python 2 中加载 Python 3 Pickled SKlearn 模型

[英]How to load Python 3 Pickled SKlearn Model in Python 2

I have a Python 3.6 script that trains an SKLearn model and then saves the model using the following code:我有一个 Python 3.6 脚本,用于训练 SKLearn 模型,然后使用以下代码保存模型:

with open('filepath', 'wb') as f:
    pickle.dump(trained_model, f, protocol=2)

When I try to load the pickle in python 3.6, things work out just fine:当我尝试在 python 3.6 中加载泡菜时,一切正常:

>>with open('filepath', 'rb') as f:
>>    model = pickle.load(f)
>>
>>model

RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
        max_depth=None, max_features='auto', max_leaf_nodes=None,
        min_impurity_decrease=0.0, min_impurity_split=None,
        min_samples_leaf=1, min_samples_split=2,
        min_weight_fraction_leaf=0.0, n_estimators=80, n_jobs=1,
        oob_score=False, random_state=None, verbose=0,
        warm_start=False)

when I run this same pickle.load command in Python 2.7, I get the following error:当我在 Python 2.7 中运行相同的 pickle.load 命令时,出现以下错误:

>>with open('filepath', 'rb') as f:
>>    model = pickle.load(f)

ValueError: non-string names in Numpy dtype unpickling

Looking at documentation and similar cases, setting protocol to 2 should make the pickle file compatible.查看文档和类似案例,将协议设置为 2应该使泡菜文件兼容。 What is causing this issue and how can I work around it?是什么导致了这个问题,我该如何解决?

You can use pickle._load() instead of .load() to force using a pure-Python implementation and get a more useful traceback.您可以使用pickle._load()代替.load()使用纯Python实现力,并获得更多有用的回溯。

If the faulty part is in numpy 's code though, you're still left to using a C debugger or tracing the source code by hand...如果错误部分在numpy的代码中,您仍然需要使用 C 调试器或手动跟踪源代码......
...Or usingnumpy pickle format spec on the part that is fed to numpy 's unpickling routine and try to guess what is wrong with it! ...或者在馈入numpy的 unpickle 例程的部分上使用numpy pickle 格式规范,并尝试猜测它有什么问题!

  • pickletools.dis() does this for you! pickletools.dis()为你做这件事! It prints a disassembly of pickle data, complete with offsets.它打印了pickle 数据的反汇编,并带有偏移量。 Though you might still need the spec to find out the nature of the violation.尽管您可能仍然需要规范来找出违规的性质。

That said, 3.4.也就是说, 3.4。 Model persistence — scikit-learn 0.19.1 documentation does warn that loading model data in another version and/or architecture is not supported and suggests saving source material instead. 模型持久性——scikit-learn 0.19.1 文档确实警告不支持在另一个版本和/或架构中加载模型数据,并建议保存源材料。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM