我在Python2中保存并腌制了一个对象。 类型的对象是sklearn的RandomForestClassifier。 我试图在Python3中解除锁定,但出现错误:
with open(model_name, "rb") as model_file:
model = pickle.load(model_file, encoding='latin1')
但是我得到了错误:
~/.local/lib/python3.6/site-packages/dill/dill.py in _load_type(name)
544
545 def _load_type(name):
--> 546 return _reverse_typemap[name]
547
548 def _create_type(typeobj, *args):
KeyError: 'ObjectType'
我尝试了以下修复程序:
def convert(old_pkl):
"""
Convert a Python 2 pickle to Python 3
"""
# Make a name for the new pickle
new_pkl = os.path.splitext(os.path.basename(old_pkl))[0]+"_p3.pkl"
# Convert Python 2 "ObjectType" to Python 3 object
dill._dill._reverse_typemap["ObjectType"] = object
# Open the pickle using latin1 encoding
with open(old_pkl, "rb") as f:
loaded = pickle.load(f, encoding="latin1")
# Re-save as Python 3 pickle
with open(new_pkl, "wb") as outfile:
pickle.dump(loaded, outfile)
convert(classifier_model_name)
但是在loaded = pickle.load(f, encoding="latin1")
行上出现了相同的错误
我该如何解决?
谢谢