No module named 'sklearn.svm._classes' when loading model from colab

peer picture peer · Apr 25, 2020 · Viewed 7k times · Source

I've trained a model on google colab and want to load it on my local machine. But I get ModuleNotFoundError: No module named 'sklearn.svm._classes'. Loading the model on colab, is no problem.

colab:

[1] import sys
    sys.version
'3.6.9 (default, Nov  7 2019, 10:44:02) \n[GCC 8.3.0]'
[2] import joblib
    import numpy as np
    from sklearn import svm
    clf = svm.SVC(gamma=0.001)
    clf.fit(np.random.rand(9,8).astype(int), np.arange(9))
    joblib.dump(clf, 'simple_classifier')
[3] joblib.load('simple_classifier')

My local machine:

>>> import sys
>>> sys.version
'3.6.9 (default, Nov  7 2019, 10:44:02) \n[GCC 8.3.0]'
>>> import numpy as np
>>> import joblib
>>> from sklearn import svm
>>> joblib.load('simple_classifier')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/.../ml-env/lib/python3.6/site-packages/joblib/numpy_pickle.py", line 605, in load
    obj = _unpickle(fobj, filename, mmap_mode)
  File "/home/.../ml-env/lib/python3.6/site-packages/joblib/numpy_pickle.py", line 529, in _unpickle
    obj = unpickler.load()
  File "/usr/lib/python3.6/pickle.py", line 1050, in load
    dispatch[key[0]](self)
  File "/usr/lib/python3.6/pickle.py", line 1338, in load_global
    klass = self.find_class(module, name)
  File "/usr/lib/python3.6/pickle.py", line 1388, in find_class
    __import__(module, level=0)
ModuleNotFoundError: No module named 'sklearn.svm._classes'

Answer

jakevdp picture jakevdp · Apr 26, 2020

Serialization via joblib will only work if the versions of installed packages are exactly the same between the program saving the model and the program loading it.

From the error you are seeing, I suspect that you are using different versions of scikit-learn on Colab and on your local machine. Make sure versions of relvant packages match, and then loading the model should work.

See https://joblib.readthedocs.io/en/latest/persistence.html for more information.