Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
like image 597
peer Avatar asked Apr 25 '20 21:04

peer


People also ask

Is there a module named 'sklearn' in sklearn?

modulenotfounderror: No module named 'sklearn.metrics'; 'sklearn' is not a package import sklearn ModuleNotFoundError: No module named 'sklearn' from sklearn.neural_network import MLPClassifier ModuleNotFoundError: No module named 'sklearn.neural_network'; 'sklearn' is not a package

Is it possible to load model on Colab with modulenotfounderror?

But I get ModuleNotFoundError: No module named 'sklearn.svm._classes'. Loading the model on colab, is no problem. colab:

Why can't I deserialize a sklearn object?

This very specific problem occurs when there is sklearn version mismatch. For example, trying to deserialize a sklearn (>= 0.22.X) object dumped with another sklearn version < 0.22.X. Sklearn introduced a change between those version, check their release website for mo information Show activity on this post.

Will serialization via JOBLIB work with different versions of scikit-learn?

8 Serialization via joblibwill only work if the versions of installed packages are exactlythe 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.


2 Answers

Serialization via joblib will only work if the versions of installed packages correspond exactly 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 Co-Lab and your local machine. Make sure versions of relevant packages match, and you should be able to load the model.

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

like image 176
jakevdp Avatar answered Oct 18 '22 06:10

jakevdp


Upgrade the sklearn library using:

pip install scikit-learn --upgrade

Occurs when the model was built with a specific version of the library and tested with other versions.

like image 3
Santosh Gunashekar Avatar answered Oct 18 '22 05:10

Santosh Gunashekar