Cannot import name 'available_if' from 'sklearn.utils.metaestimators'

Viewed 7797

While importing "from imblearn.over_sampling import SMOTE", getting import error. Please check and help. I tried upgrading sklearn, but the upgrade was undone with 'OSError'. Firsty installed imbalance-learn through pip.

!pip install -U imbalanced-learn
  • Using jupyter notebook

  • Windows 10

  • sklearn version - 0.24.1

  • numpy version - 1.19.5

     --------------------------------------------------------------------------
     ImportError                               Traceback (most recent call last)
    

    in ----> 1 from imblearn.over_sampling import SMOTE

    ~\anaconda3\lib\site-packages\imblearn_init_.py in 35 import types 36 ---> 37 from . import combine 38 from . import ensemble 39 from . import exceptions

    ~\anaconda3\lib\site-packages\imblearn\combine_init_.py in 3 """ 4 ----> 5 from ._smote_enn import SMOTEENN 6 from ._smote_tomek import SMOTETomek 7

    ~\anaconda3\lib\site-packages\imblearn\combine_smote_enn.py in 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

    ~\anaconda3\lib\site-packages\imblearn\base.py in 13 from sklearn.utils.multiclass import check_classification_targets 14 ---> 15 from .utils import check_sampling_strategy, check_target_type 16 from .utils._validation import ArraysTransformer 17 from .utils._validation import _deprecate_positional_args

    ~\anaconda3\lib\site-packages\imblearn\utils_init_.py in 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

    ~\anaconda3\lib\site-packages\imblearn\utils_validation.py in 14 from sklearn.base import clone 15 from sklearn.neighbors._base import KNeighborsMixin ---> 16 from sklearn.neighbors import NearestNeighbors 17 from sklearn.utils import column_or_1d 18 from sklearn.utils.multiclass import type_of_target

    ~\anaconda3\lib\site-packages\sklearn\neighbors_init_.py in 14 from ._nearest_centroid import NearestCentroid 15 from ._kde import KernelDensity ---> 16 from ._lof import LocalOutlierFactor 17 from ._nca import NeighborhoodComponentsAnalysis 18 from ._base import VALID_METRICS, VALID_METRICS_SPARSE

    ~\anaconda3\lib\site-packages\sklearn\neighbors_lof.py in 10 from ..base import OutlierMixin 11 ---> 12 from ..utils.metaestimators import available_if 13 from ..utils.validation import check_is_fitted 14 from ..utils import check_array

    ImportError: cannot import name 'available_if' from 'sklearn.utils.metaestimators' (C:\Users\dks_m\anaconda3\lib\site-packages\sklearn\utils\metaestimators.py)

5 Answers

IF in jupyter , restart the kernel.This fixed!

I believe the issue is with python versioning of scikit-learn. I was able to resolve by reinstalling the Python3 version:

pip uninstall scikit-learn -y

pip3 install scikit-learn 

Remember to restart terminal/notebook after package updates.

This gives me scikit-learn v1.0.2 which resolves error in Python3

Try using anaconda prompt for installation. It works for me.

That usually happens when 2 different versions of packages do not match. If you are using the jupyter notebook, restarting your environment is going to solve your issue.

Good day all. What helped me is installing pycaret=='2.3.10 ' and scikit-learn='0.23.2' at the same time. These two version are compatible and all works fine. I installed scikit-learn using conda as the older versions are not available through pip, and I installed Pycaret using pip3. I hope this helps all who have struggled to get this working like I did.

Related