Solvedyellowbrick 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


mportError: cannot import name 'safe_indexing' from 'sklearn.utils' (~/.venv/lib/python3.8/site-packages/sklearn/utils/
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/ in <module>
     37 from .anscombe import anscombe
     38 from .datasaurus import datasaurus
---> 39 from .classifier import ROCAUC, ClassBalance, ClassificationScoreVisualizer
     41 # from .classifier import crplot, rocplot
~/.venv/lib/python3.8/site-packages/yellowbrick/classifier/ 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
~/Repositories/player-similarity-clusters/.venv/lib/python3.8/site-packages/yellowbrick/classifier/ 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
ImportError: cannot import name 'safe_indexing' from 'sklearn.utils' (~/.venv/lib/python3.8/site-packages/sklearn/utils/

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
21 Answers

✔️Accepted Answer

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 to something like the below and test that it works properly:

    # 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!

Related Issues:

yellowbrick ImportError: cannot import name 'safe_indexing' from 'sklearn.utils'
Hi @Paulj1989 and thanks for letting us know! It looks like scikit-learn has again changed their pub...
spyder Spyder Won't Launch
Could you run in a terminal and try again? Description of your problem What steps will reproduce the...
spyder [warn] kq_init: detected broken kqueue
Could you run in a terminal and try again? Description of your problem What steps will reproduce the...
spyder Mac OS spyder won't launch: directory mismatch
Please run to see if that helps. Problem Description Spyder won't launch ...
spyder Spyder crashes with segmentation fault on startup in Anaconda 4.5.0
Following a suggestion in #3326 I was able to work around the problem by doing This problem is also ...
spyder qtpy.PythonQtError: No Qt bindings could be found
You need to install PyQt4 or PyQt5 because that's not installed automatically by pip For PyQt5 just ...
spyder “RuntimeError: This event loop is already running”; debugging aiohttp, asyncio and IDE “spyder3” in python 3.6.5
I got the issue resolved by using the nest_async pip install nest_asyncio and adding below lines in ...
spyder Getting "Spyder already running" warning when it's not
You need to look for a directory called .spyder2 in your Users\<your account> directory the find a f...
spyder Autocomplete is not working
@hepinjing could you remove enum and try again? Description In the last version of Anaconda 3 ...
spyder Spyder doesn't start - no module named 'PyQt5.QtWebEngineWidgets'
Actually you need to uninstall it first: pip uninstall pyqtwebengine pip install pyqtwebengine This ...
spyder Segmentation fault (core dumped) when trying to start conda-installed spyder 3.2.8
@addyblue Try conda install pyopengl Developer Note: *This issue is now resolved in Spyder >=3.3.0 T...
anaconda Getting error: 'Autoformatting failed, buffer not changed'
I guess is because almost every single line on that file contains a PEP8 violation ...
spyder spyder 3.2.8 crashes on launch with (clean conda install of) Python 3.6.5 on MacOS 10.12.6
The real solution is to run in Problem Description spyder on MacOS 10.12.6 with Python ...
spyder PyQt 5 issue in spyder
@ciornav what happens if instead of sys.exit(app.exec_()) you only use app.exec_()? ...
spyder Can't open Spyder even after fresh Anaconda install/in new environment
@CAM-Gerlach Thank you very much it worked for me! To sum up Description of your problem What steps ...
spyder Run cell (ctrl+Enter) don't work in Spyder inatalled with the Anaconda
No individual lines are run with F9. Issue Report Checklist Searched the issues page for similar rep...
spyder Problems with running turtle programs in Spyder
@ccordoba12 testing a little bit with Turtle I came with some tips to run a script multiple times in...
spyder Spyder forces discrete graphics on OSX
Hello! I encountered the very same issue and have found what so far seems to be an effective resolut...
spyder An error ocurred while starting the kernel: b''
I had the same problem with the following error and spent a few days searching the net for solutions...
keras Loading model with custom loss function: ValueError: 'Unknown loss function'
I solved this problem by adding 'custom_bojects' my loss function: I trained and saved a model that ...
keras Tensorflow backend - bug in model._make_predict_function(...)
I had this problem when doing inference in a different thread than where I loaded my model ...
keras Creating Model variable throws "AttributeError: 'Tensor' object has no attribute '_keras_history'"
@zaid478 oh I assumed you were doing it on coursera Anyway Hello all setup: Keras 2.0 Tensorflow 1.0...
tensorflow ImportError: cannot open shared object file: No such file or directory
@rhinsall I just found this out myself not sure if it's common knowledge but got around this by doin...
keras "<tensor> is not an element of this graph." when loading model.
I had a problem similar to that of @piraka9011 which was solved by calling model._make_predict_funct...
pytorch I can't import PyTorch, libomp.dylib can't be loaded.
brew install libomp solves the problem. 🐛 Bug I tried to install PyTorch but I can't use it ...
pytorch RuntimeError: cuda runtime error (59) : device-side assert triggered when running transfer_learning_tutorial
@soumith Hi I find the error it is due to my labels contain value -1 which is unacceptable in criter...
tensorflow At Runtime : "Error while reading resource variable softmax/kernel from Container: localhost"
I had the same issue in tensorflow 1.13.1 which I have resolved by creating a reference to the sessi...
tensorflow Mac + Python 3.6.1: Attempting to download mnist data results in CERTIFICATE_VERIFY_FAILED error
This might have also been because I recently switched to a new machine and it's still only partially...
tensorflow UnrecognizedFlagError: Unknown command line flag 'f'
I solved this issue by adding the line:'f' '' 'kernel') what this essent...
tensorflow FutureWarning: Deprecated numpy API calls in tf.python.framework.dtypes
@Akeaakar And for the time being pip install numpy<1.17 to revert to numpy version 1.16.4 ...
tensorflow ERROR: Cannot uninstall 'wrapt'. during upgrade
This worked for me: pip install wrapt --upgrade --ignore-installed pip install tensorflow ...
pytorch ModuleNotFoundError: No module named 'torch._C'
Solution the problem is that you have a folder called torch in the same directory which is being pic...
tensorflow Keras application - Tensor is not an element of this graph on eval after train
K.clear_session() did not work for me however what worked was : While predicting use the same graph ...
xgboost Problems while installing in OSX 10.11.6?
Success! 🎉 I found this here: clang LLVM compiler on Mac OS X from Xcode doesn't support OpenMP mul...
keras Quick Question: can a model be fit for multiple times?
Yes successive calls to fit will incrementally train the model. I'm now having something around 5000...
tensorflow InternalError: Blas GEMM launch failed
Make sure you have no other processes using the GPU running Run nvidia-smi to check this. ...
pytorch RuntimeError: DataLoader worker is killed by signal: Killed.
I've encountered the same problem recently If you're using the docker to run the PyTorch program wit...
tensorflow Is python 3.7.x supported with Tensorflow
I sill get Ive been trying to install Tensorflow on my computer which currently runs python 3.7 howe...
tensorflow Tensorflow installation Error in Mac Sierra due to numpy
Also Had to do this with the six dependency as well Hi I want to install TensorFlow on Mac Currently...
keras Error when checking model target: expected activation_2 to have shape (None, 10) but got array with shape (3, 1)
Your model has an output of shape (10,) however your outputs have dimension (1,) You probably want t...
tensorflow tensorboard: view graph from saved_model.pb file [feature request]
@brandondutra As far as I know you don't need to create any summaries to load the graph into Tensorb...
keras Is there a way in Keras to apply different weights to a cost function?
Ok so I had the time to quickly test it Hi there I am trying to implement a classification problem w...
tensorflow ImportError: cannot open shared object file: No such file or directory
Double check that you have exported the LD_LIBRARY_PATH export LD_LIBRARY_PATH=/usr/local/cuda-8.0/l...
pytorch [PyTorch][Feature Request] Label Smoothing for CrossEntropyLoss
Here is my implement Hi guys The type torch.LongTensor of target will hinder the implementation like...
caffe version `GLIBCXX_3.4.21' not found
^ Should fix this error Please ask installation questions only in the Caffe mailing list. ...
keras Keras model cannot be loaded if it contains a Lambda layer calling tf.image.resize_images
Try using custom_objects argument in load_model (or model_from_json) function For your case load_mod...
keras how to use fit_generator with multiple image inputs
Thanks Here is how I solved it following some ideas from issue 3386 maybe someone might find it usef...