来自python3的python 2中的unpickle sklearn.tree.DescisionTreeRegressor

时间:2017-01-18 13:39:46

标签: python-2.7 python-3.x numpy scikit-learn pickle

我想在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)

酸洗之后我尝试在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)

但它会引发错误:

---------------------------------------------------------------------------
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

我们有没有在python 2 DecisionTreeRegressor中进行unpickle的方法,它在python 3中被腌制?

0 个答案:

没有答案