DistrictDataLabs / yellowbrick

Visual analysis and diagnostic tools to facilitate machine learning model selection.
http://www.scikit-yb.org/
Apache License 2.0
4.3k stars 559 forks source link

KElbowVisualizer wrapping functionality does not work with notebook rendering of estimator #1232

Closed vtphan closed 2 years ago

vtphan commented 2 years ago

Describe the bug In Jupyter Notebook, this code produces this error:

AttributeError: 'KMeans' object has no attribute 'k'

To Reproduce

from sklearn.cluster import KMeans
from yellowbrick.cluster import KElbowVisualizer
visualizer = KElbowVisualizer(KMeans(), k=(2,12))
visualizer

Additional context The use case above is just an example. The bug shows in other complex use cases.

bbengfort commented 2 years ago

@lwgray I was able to reproduce this error on:

Python: 3.10.2 Yellowbrick: develop branch

Dependencies:

matplotlib==3.5.2
scipy==1.8.1
scikit-learn==1.1.1
numpy==1.22.4
cycler==0.11.0

Taking a look at it now.

bbengfort commented 2 years ago

@vtphan @lwgray this appears to be related to the new scikit-learn print functionality that attempts to render Estimators using matplotlib with interactive dropdowns, e.g.

Screen Shot 2022-05-21 at 11 47 56

Because the last line of the cell is visualizer the notebook is trying to render it as output and because the visualizer extends Estimator it goes through the scikit-learn pretty drawing functionality.

@vtphan this bug does not affect the behavior of the visualizer; you can still fit and show the visual image, but I can see how it can be annoying, so it's something we'll want to try to deal with.

bbengfort commented 2 years ago

It looks like this might be a subclassing issue; ModelVisualizer renders just fine:

Screen Shot 2022-05-21 at 12 28 49

bbengfort commented 2 years ago

Turns out this is specifically a problem with KElbowVisualizer -- PR fix coming.