YellowBrick ImportError: cannot import name 'safe_indexing' from 'sklearn.utils'
Asked Answered
D

3

8

I'm trying to plot a silhouette plot for a K-Means model I've run, however, I get the error: ImportError: cannot import name 'safe_indexing' from 'sklearn.utils.

I was initially getting the NotFoundError issue described in this post here however I got this issue once I fixed it. My code is below:

from yellowbrick.cluster import SilhouetteVisualizer

fig, ax = plt.subplots(2, 2, figsize=(15,8))
for i in range(1,6):
    
    kmeans = KMeans(n_clusters=i, init='k-means++', n_init=10, max_iter=100, random_state=0)
    q, mod = divmod(i, 2)
  
    visualizer = SilhouetteVisualizer(km, colors='yellowbrick', ax=ax[q-1][mod])
    visualizer.fit(ct1)
Deen answered 6/1, 2021 at 19:17 Comment(0)
D
6

In python3.7 with yellowbrick v1.2 and scikit-learn v0.23.2 I don't get the error but when I bump scikit-learn to v0.24.0 I get the same ImportError.

I checked the API documentation and, like in the link you referenced, in sklearn v0.22.0 they deprecated utils.safe_indexing to private (utils._safe_indexing). But I don't get the import error with yellowbrick v1.2 and v0.22.0<=sklearn<=v0.23.2 installed. I think they just finally removed the public utils.safe_indexing in v0.24.0 so if you just install a version of scikit-learn before v0.24.0 the import should work.

Edit: Here is yellowbrick's github issue if you want to track their progress on a workaround or update for this problem

Debbradebby answered 9/1, 2021 at 3:58 Comment(0)
W
0

For my case, i uninstalled the yellowbrick package inside the project env (that was installed via conda install..)and then reinstalled using pip install, and it worked.

Whitecap answered 12/8, 2022 at 17:4 Comment(0)
C
-2

I have also encountered this situation, and talk about my solution.

I used python3.8,yellowbrick v1.2 and scikit-learn v0.24.0, then I changed to scikit-learn v0.23.2 as adbailey said, but it seems nothing changes.

So, I try to run it on 'Colab'. Well, it works now. The part requirements of Colab is as follow:

...
pandas==1.1.5
scikit-learn==0.22.2
scipy==1.4.1
...
yellowbrick==0.9.1
...

In other words, it is no problem to refer to this configuration.

pip install yellowbrick==0.9.1 scikit-learn==0.22.2

Clemenceau answered 4/6, 2021 at 2:13 Comment(1)
While this may have worked, one should not need to regress to v0.9.Blinking

© 2022 - 2024 — McMap. All rights reserved.