unpickle sklearn.tree.DescisionTreeRegressor in python 2 from python3

孤街浪徒 提交于 2019-12-25 08:43:12

问题


I wanna fit model in python 3.5 (numpy 1.11.2, sklearn 0.18.1)

import pickle
from sklearn.tree import DecisionTreeRegressor
clf = DecisionTreeRegressor()
X = np.array([[1,2,3,4],[1,1,2,2],[1,2,1,2]]).T 
y = [1,1,0,0]
clf.fit(X,y)
with open(join(path_to_data, 'models', 'debug.model'), 'wb') as f: 
    pickle.dump(clf, f, protocol=2)

After pickling I try to unpickle model in python 2.7 (numpy 1.11.2, sklearn 0.18.1)

import pickle
with open(join(path_to_data, 'models', 'debug.model'), 'rb') as f: 
        clf = pickle.load(f)

but it raise error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-78-2eaf35b8e6d9> in <module>()
----> 1 joblib.load(join(path_to_data,'models','queryforest_debug.model'))

/home/iiivanitskiy/.local/lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle.pyc in load(filename, mmap_mode)
    573                     return load_compatibility(fobj)
    574 
--> 575                 obj = _unpickle(fobj, filename, mmap_mode)
    576 
    577     return obj

/home/iiivanitskiy/.local/lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle.pyc in _unpickle(fobj, filename, mmap_mode)
    505     obj = None
    506     try:
--> 507         obj = unpickler.load()
    508         if unpickler.compat_mode:
    509             warnings.warn("The file '%s' has been generated with a "

/usr/lib/python2.7/pickle.pyc in load(self)
    856             while 1:
    857                 key = read(1)
--> 858                 dispatch[key](self)
    859         except _Stop, stopinst:
    860             return stopinst.value

/home/iiivanitskiy/.local/lib/python2.7/site-packages/sklearn/externals/joblib/numpy_pickle.pyc in load_build(self)
    325         NDArrayWrapper is used for backward compatibility with joblib <= 0.9.
    326         """
--> 327         Unpickler.load_build(self)
    328 
    329         # For backward compatibility, we support NDArrayWrapper objects.

/usr/lib/python2.7/pickle.pyc in load_build(self)
   1215         setstate = getattr(inst, "__setstate__", None)
   1216         if setstate:
-> 1217             setstate(state)
   1218             return
   1219         slotstate = None

ValueError: non-string names in Numpy dtype unpickling

Do we have the way to unpickle in python 2 DecisionTreeRegressor, which was pickled in python 3?

来源:https://stackoverflow.com/questions/41720952/unpickle-sklearn-tree-descisiontreeregressor-in-python-2-from-python3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!