Skip to content

Joblib objects from python 3.6 not loadable in 3.7 #13215

Closed
@EmreAtes

Description

@EmreAtes

Description

KeyError when loading Random Forest generated by python 3.6 from python 3.7. I opened #13191 for it but then learned that it's not the expected behavior.

Steps/Code to Reproduce

Run store.py in python3.6, and load.py in python3.7:

store.py:

import sys    
import sklearn    
from sklearn.ensemble import RandomForestClassifier    
from sklearn.datasets import make_classification    
from sklearn.externals import joblib    
    
X, y = make_classification(n_samples=1000, n_features=4,    
                           n_informative=2, n_redundant=0,    
                           random_state=0, shuffle=False)    
clf = RandomForestClassifier(n_estimators=100, max_depth=2,    
                             random_state=0)    
clf.fit(X, y)    
    
print("Python verion: {}".format(sys.version))    
print("Scikit verion: {}".format(sklearn.__version__))    
print("Joblib verion: {}".format(joblib.__version__))    
print(clf.feature_importances_)    
print(clf.predict([[0, 0, 0, 0]]))    
    
joblib.dump(clf, './RandomForest.pkl')  

load.py:

import sys    
import sklearn    
from sklearn.externals import joblib    
     
print("Python verion: {}".format(sys.version))    
print("Scikit verion: {}".format(sklearn.__version__))    
print("Joblib verion: {}".format(joblib.__version__))    
     
clf = joblib.load('./RandomForest.pkl')    
     
print(clf.feature_importances_)    
print(clf.predict([[0, 0, 0, 0]]))   

Expected Results

The results are the same for loading/storing using only python3.6 or only python3.7:

(test) $ ./store.py 
/home/ates/.virtualenvs/test/lib/python3.6/site-packages/sklearn/ensemble/weight_boosting.py:29: DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release.
  from numpy.core.umath_tests import inner1d
Python verion: 3.6.8 (default, Jan 27 2019, 09:00:23) 
[GCC 8.2.1 20181215 (Red Hat 8.2.1-6)]
Scikit verion: 0.19.1
Joblib verion: 0.11
[0.14205973 0.76664038 0.0282433  0.06305659]
[1]
(test) $ ./load.py 
Python verion: 3.6.8 (default, Jan 27 2019, 09:00:23) 
[GCC 8.2.1 20181215 (Red Hat 8.2.1-6)]
Scikit verion: 0.19.1
Joblib verion: 0.11
/home/ates/.virtualenvs/test/lib/python3.6/site-packages/sklearn/ensemble/weight_boosting.py:29: DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release.
  from numpy.core.umath_tests import inner1d
[0.14205973 0.76664038 0.0282433  0.06305659]
[1]
(test) $ 

Actual Results

(test) $ ./store.py 
/home/ates/.virtualenvs/test/lib/python3.6/site-packages/sklearn/ensemble/weight_boosting.py:29: DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release.
  from numpy.core.umath_tests import inner1d
Python verion: 3.6.8 (default, Jan 27 2019, 09:00:23) 
[GCC 8.2.1 20181215 (Red Hat 8.2.1-6)]
Scikit verion: 0.19.1
Joblib verion: 0.11
[0.14205973 0.76664038 0.0282433  0.06305659]
[1]
(test) $ deactivate
$ ./load.py 
/usr/lib64/python3.7/site-packages/sklearn/externals/six.py:7: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
/usr/lib64/python3.7/site-packages/sklearn/utils/__init__.py:4: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Sequence
Python verion: 3.7.2 (default, Jan 16 2019, 19:49:22) 
[GCC 8.2.1 20181215 (Red Hat 8.2.1-6)]
Scikit verion: 0.19.1
Joblib verion: 0.11
/usr/lib64/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:29: DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release.
  from numpy.core.umath_tests import inner1d
Traceback (most recent call last):
  File "./load.py", line 11, in <module>
    clf = joblib.load('./RandomForest.pkl')
  File "/usr/lib/python3.7/site-packages/joblib/numpy_pickle.py", line 578, in load
    obj = _unpickle(fobj, filename, mmap_mode)
  File "/usr/lib/python3.7/site-packages/joblib/numpy_pickle.py", line 508, in _unpickle
    obj = unpickler.load()
  File "/usr/lib64/python3.7/pickle.py", line 1085, in load
    dispatch[key[0]](self)
KeyError: 0
$ 

Versions

$ python3 version.py 
Linux-4.20.8-200.fc29.x86_64-x86_64-with-fedora-29-Twenty_Nine
Python 3.7.2 (default, Jan 16 2019, 19:49:22) 
[GCC 8.2.1 20181215 (Red Hat 8.2.1-6)]
NumPy 1.15.1
SciPy 1.1.0
/usr/lib64/python3.7/site-packages/sklearn/externals/six.py:7: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
/usr/lib64/python3.7/site-packages/sklearn/utils/__init__.py:4: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Sequence
Scikit-Learn 0.19.1
$ workon test
(test) $ python3 version.py 
Linux-4.20.8-200.fc29.x86_64-x86_64-with-fedora-29-Twenty_Nine
Python 3.6.8 (default, Jan 27 2019, 09:00:23) 
[GCC 8.2.1 20181215 (Red Hat 8.2.1-6)]
NumPy 1.15.1
SciPy 1.1.0
Scikit-Learn 0.19.1
(test) $ 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions