yellowbrick: ImportError: cannot import name 'safe_indexing' from 'sklearn.utils'

Describe the bug

I’m trying to import KElbowVisualizer from yellowbrick.cluster, and it is returning the following error:

ImportError: cannot import name 'safe_indexing' from 'sklearn.utils'

To Reproduce

import pandas as pd
from sklearn.cluster import KMeans
from yellowbrick.cluster import KElbowVisualizer

Traceback

mportError: cannot import name 'safe_indexing' from 'sklearn.utils' (~/.venv/lib/python3.8/site-packages/sklearn/utils/__init__.py)
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-3-eb5694100c70> in <module>
      1 import pandas as pd
----> 2 from yellowbrick.cluster import KElbowVisualizer
~/.venv/lib/python3.8/site-packages/yellowbrick/__init__.py in <module>
     37 from .anscombe import anscombe
     38 from .datasaurus import datasaurus
---> 39 from .classifier import ROCAUC, ClassBalance, ClassificationScoreVisualizer
     40 
     41 # from .classifier import crplot, rocplot
~/.venv/lib/python3.8/site-packages/yellowbrick/classifier/__init__.py in <module>
     28 from .confusion_matrix import ConfusionMatrix, confusion_matrix
     29 from .rocauc import ROCAUC, roc_auc
---> 30 from .threshold import DiscriminationThreshold, discrimination_threshold
     31 from .prcurve import PrecisionRecallCurve, PRCurve, precision_recall_curve
     32 
~/Repositories/player-similarity-clusters/.venv/lib/python3.8/site-packages/yellowbrick/classifier/threshold.py in <module>
     28 from sklearn.model_selection import ShuffleSplit
     29 from sklearn.metrics import precision_recall_curve
---> 30 from sklearn.utils import indexable, safe_indexing
     31 from sklearn.utils.multiclass import type_of_target
     32 
ImportError: cannot import name 'safe_indexing' from 'sklearn.utils' (~/.venv/lib/python3.8/site-packages/sklearn/utils/__init__.py)

Desktop (please complete the following information):

  • OS: Ubuntu 20.04
  • Python Version: 3.8.5
  • Yellowbrick Version: 1.2

Additional context

  • skleaarn Version: 0.24.0

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 3
  • Comments: 21 (11 by maintainers)

Most upvoted comments

Hi @Paulj1989 and thanks for letting us know!

It looks like scikit-learn has again changed their public/private API, so utils.safe_indexing is now called utils._safe_indexing. In order for the utility to work in Yellowbrick, we will have to change our usage of safe_indexing to support users with versions of scikit-learn >= 0.24 without deprecating support for users with versions of scikit-learn < 0.24. We’ll need to change our import statement for safe_indexing at the top of threshold.py to something like the below and test that it works properly:

try:
    # See #1137: this allows compatibility for scikit-learn >= 0.24
    from sklearn.utils import safe_indexing
except ImportError:
    from sklearn.utils import _safe_indexing

If you would like to open a PR to work on this, let us know! We are a small team of unpaid volunteers who work on Yellowbrick in our spare time, so we’d welcome your help. In the meantime, you can avoid the error by downgrading your version of scikit-learn to <0.24

Thanks again for checking out Yellowbrick and giving us a heads up about the new sklearn API changes!

@rrsquez I think you may be misunderstanding the purpose of our GitHub issues - this is the place where users of Yellowbrick report bugs to the coders and maintainers who work on Yellowbrick. Note that the maintainers here are professional software engineers, data scientists, and educators who develop Yellowbrick in their free time as an open-source project and not for pay. Although they are happy to answer your questions, they cannot read your mind or see your computer and so must provide debugging suggestions for you to work through. In your case, your problem had nothing to do with Yellowbrick, but with your own Python environment.

Your comment is disrespectful and inappropriate and contributed nothing constructive to this discussion. I would ask that you please apologize to @ShahbazT, @rebeccabilbro, and @Paulj1989 if you’d like to continue to be a part of our community. In the future, please consider your words more carefully and speak with respect as described by our code of conduct. Failure to abide by these requests will result in our blocking you from this repository.

@rebeccabilbro thank you so much for your help and activity on this thread. I had this issue as well and just by reading your responses to other users i was able to figure it out. in my case “conda install -c conda-forge yellowbrick” did the trick and updated my YB from 1.2 to 1.3.post1.

One humble request that I was wondering if I could have is that if it is possible for you and your team, add a save figure option to the module as well. I am running my code on a super computer and it lacks the ability to show graphics and can only save figures. if I could save my figures without the need to show them then my codes would be able to run on faster computers

thank you again for your time and considerations and best regards

Hello @rrsquez and thanks for checking out our project. Apologies that you’re having difficulties with the dependencies. Would you mind providing a few more details to see if we can help?

OS: (sounds like windows?) Python version: Yellowbrick version: scikit-learn version:

@rebeccabilbro Excuse me for one minute, I’m new to use Yellowbrick and meet the same issue. I managed to solve it by conda install yellowbrick=1.3. However, this issue came after I had followed the quick start conda install -c districtdatalabs yellowbrick from https://www.scikit-yb.org/en/latest/quickstart.html Is it the official website of YB? If so, I suggest that the instruction may need to add some tips or more people will suffer from this. Huge thanks for your great work!

@rebeccabilbro: I’m happy to report that your suggestion worked (i.e. .show(outpath=“path.png”)) =D Thank you!

@rebeccabilbro: I’d like to mirror the sentiments of gratitude by @alirezaro93 and also thank them for providing the conda-forge update command.

Also, @rebeccabilbro, I’m in the process of testing the very code you suggested (i.e. .show(outpath=“path.png”)) - I found the outpath option in the documentation 😃 I’ll report back if the snippet works as intended.

Hello @rrsquez and thanks for checking out our project. Apologies that you’re having difficulties with the dependencies. Would you mind providing a few more details to see if we can help?

OS: (sounds like windows?) Python version: Yellowbrick version: scikit-learn version:

Hi @rebeccabilbro

I have the same issue on Windows 10 Jupyter Notebook 6.4.0 python-dateutil 2.8.2 python-jsonrpc-server 0.4.0 python-language-server 0.36.2 python-slugify 5.0.2 yellowbrick 1.2 scikit-learn 0.24.2

From the Jupiter notebook I have also tried running import sys !{sys.executable} -m pip install mbalanced-learn

And the suggested Try / Exception lines which did not help!

Should I try to uninstall some packages and reinstall them? i.e.

conda uninstall numpy scipy scikit-learn yellowbrick -y restart kernel then conda install numpy scipy scikit-learn yellowbrick -y Resstart kernel again!

There has been a discussion for scikit-learn to have a “developer API”: https://github.com/scikit-learn/scikit-learn/issues/15801. This would include utilities such as safe_indexing. We use this internally and it has good test coverage within the sklearn code base, so we can reasonably make this public and support it. It would be interesting to see what other functions in sklearn.utils are useful for downstream libraries.

Happy to give it a go @rebeccabilbro. Will report back if I hit any issues in the process.

Thanks for your help!