No module named 'sklearn.neighbors._base'
Asked Answered
F

4

15

I have recently installed imblearn package in jupyter using

!pip show imbalanced-learn

But I am not able to import this package.

from tensorflow.keras import backend
from imblearn.over_sampling import SMOTE

I get the following error

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-20-f19c5a0e54af> in <module>
      1 # from sklearn.utils import resample
      2 from tensorflow.keras import backend
----> 3 from imblearn.over_sampling import SMOTE
      4 
      5 

~/.virtualenvs/p3/lib/python3.6/site-packages/imblearn/__init__.py in <module>
     32     Module which allowing to create pipeline with scikit-learn estimators.
     33 """
---> 34 from . import combine
     35 from . import ensemble
     36 from . import exceptions

~/.virtualenvs/p3/lib/python3.6/site-packages/imblearn/combine/__init__.py in <module>
      3 """
      4 
----> 5 from ._smote_enn import SMOTEENN
      6 from ._smote_tomek import SMOTETomek
      7 

~/.virtualenvs/p3/lib/python3.6/site-packages/imblearn/combine/_smote_enn.py in <module>
      8 from sklearn.utils import check_X_y
      9 
---> 10 from ..base import BaseSampler
     11 from ..over_sampling import SMOTE
     12 from ..over_sampling.base import BaseOverSampler

~/.virtualenvs/p3/lib/python3.6/site-packages/imblearn/base.py in <module>
     14 from sklearn.utils.multiclass import check_classification_targets
     15 
---> 16 from .utils import check_sampling_strategy, check_target_type
     17 
     18 

~/.virtualenvs/p3/lib/python3.6/site-packages/imblearn/utils/__init__.py in <module>
      5 from ._docstring import Substitution
      6 
----> 7 from ._validation import check_neighbors_object
      8 from ._validation import check_target_type
      9 from ._validation import check_sampling_strategy

~/.virtualenvs/p3/lib/python3.6/site-packages/imblearn/utils/_validation.py in <module>
     11 
     12 from sklearn.base import clone
---> 13 from sklearn.neighbors._base import KNeighborsMixin
     14 from sklearn.neighbors import NearestNeighbors
     15 from sklearn.utils.multiclass import type_of_target

ModuleNotFoundError: No module named 'sklearn.neighbors._base'

Other packages in the environment

numpy==1.16.2
pandas==0.24.2
paramiko==2.1.1
matplotlib==2.2.4
scikit-learn==0.22.1
Keras==2.2.4
tensorflow==1.12.0
tensorboard==1.12.0
tensorflow-hub==0.4.0
xlrd==1.2.0
flask==1.0.2
wtforms==2.2.1
bs4==0.0.1
gensim==3.8.1
spacy==2.2.3
nltk==3.4.5 
wordcloud==1.6.0
pymongo==3.10.1    
imbalanced-learn==0.6.1

I checked the sklearn package, it contains base module, not _base. But modifying it may not be the right solution. Any other solution to fix this issue.

Feeder answered 10/2, 2020 at 7:14 Comment(1)
sklearn.neighbors._base has been a thing since scikit-learn 0.22.1 (github.com/scikit-learn/scikit-learn/commit/…). Are you sure you're looking at the right environment?Calculate
A
11

Previous sklearn.neighbors.base has been renamed to sklearn.neighbors._base in version 0.22.1.
You have probably a version of scikit-learn older than that. Installing the latest release solves the problem:

pip install -U scikit-learn

or

pip install scikit-learn==0.22.1

Antin answered 10/2, 2020 at 14:35 Comment(0)
D
18

If in case you want to persist with the latest version of scikit-learn, add the following code to your script or execute the following code in your environment before installing imblearn

import sklearn.neighbors._base
sys.modules['sklearn.neighbors.base'] = sklearn.neighbors._base

This has to be after

pip install sklearn

or in a notebook environment:

!pip install sklearn

This problem stems from the fact that certain modules are named with an underscore in the newer scikit-learn releases

Decorative answered 24/1, 2021 at 3:52 Comment(1)
Worked for me, I was getting the error No module named 'sklearn.neighbors.base' (in sklearn version 0.24.1) when importing missingpy.Randalrandall
A
11

Previous sklearn.neighbors.base has been renamed to sklearn.neighbors._base in version 0.22.1.
You have probably a version of scikit-learn older than that. Installing the latest release solves the problem:

pip install -U scikit-learn

or

pip install scikit-learn==0.22.1

Antin answered 10/2, 2020 at 14:35 Comment(0)
M
3

I had a similar problem trying to import SMOTE from imblearn.over_sampling and my version of scikit-learn was up to date (0.24.1). What worked for me was:

First I downgraded my scikit learn version to 0.22.1 using

 pip install scikit-learn==0.22.1

Next, I updated the imbalanced-learn package using:

pip install -U imbalanced-learn

That uninstalled scikit-learn-0.22.1, installed the updated version (scikit-learn-0.24.1), and updated the imbalanced-learn package. Everything worked fine thereafter.

Milson answered 10/4, 2021 at 9:35 Comment(0)
P
0

If it is in a particular env, you must copy the _base file or base file to the env from package file.

I had the same problem in my tensorflow env. I just copied _base and base file to my tensorflow env and worked.

Priestley answered 2/4, 2021 at 11:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.