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

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

Closed jmrichardson closed 2 years ago

jmrichardson commented 3 years ago

Using the example documentation code:

from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs

from yellowbrick.cluster import KElbowVisualizer

# Generate synthetic dataset with 8 random clusters
X, y = make_blobs(n_samples=1000, n_features=12, centers=8, random_state=42)

# Instantiate the clustering model and visualizer
model = KMeans()
visualizer = KElbowVisualizer(model, k=(4,12))

visualizer.fit(X)        # Fit the data to the visualizer
visualizer.show()        # Finalize and render the figur

I get this error:

Out[12]: Traceback (most recent call last):
  File "D:\Anaconda3\envs\tuneta\lib\site-packages\IPython\core\formatters.py", line 224, in catch_format_error
    r = method(self, *args, **kwargs)
  File "D:\Anaconda3\envs\tuneta\lib\site-packages\IPython\core\formatters.py", line 970, in __call__
    return method(include=include, exclude=exclude)
  File "D:\Anaconda3\envs\tuneta\lib\site-packages\sklearn\base.py", line 605, in _repr_mimebundle_
    output = {"text/plain": repr(self)}
  File "D:\Anaconda3\envs\tuneta\lib\site-packages\sklearn\base.py", line 274, in __repr__
    repr_ = pp.pformat(self)
  File "D:\Anaconda3\envs\tuneta\lib\pprint.py", line 153, in pformat
    self._format(object, sio, 0, 0, {}, 0)
  File "D:\Anaconda3\envs\tuneta\lib\pprint.py", line 170, in _format
    rep = self._repr(object, context, level)
  File "D:\Anaconda3\envs\tuneta\lib\pprint.py", line 404, in _repr
    repr, readable, recursive = self.format(object, context.copy(),
  File "D:\Anaconda3\envs\tuneta\lib\site-packages\sklearn\utils\_pprint.py", line 189, in format
    return _safe_repr(
  File "D:\Anaconda3\envs\tuneta\lib\site-packages\sklearn\utils\_pprint.py", line 440, in _safe_repr
    params = _changed_params(object)
  File "D:\Anaconda3\envs\tuneta\lib\site-packages\sklearn\utils\_pprint.py", line 93, in _changed_params
    params = estimator.get_params(deep=False)
  File "C:\Users\john\AppData\Roaming\Python\Python38\site-packages\yellowbrick\base.py", line 342, in get_params
    params = super(ModelVisualizer, self).get_params(deep=deep)
  File "D:\Anaconda3\envs\tuneta\lib\site-packages\sklearn\base.py", line 205, in get_params
    value = getattr(self, key)
  File "C:\Users\john\AppData\Roaming\Python\Python38\site-packages\yellowbrick\utils\wrapper.py", line 42, in __getattr__
    return getattr(self._wrapped, attr)
AttributeError: 'KMeans' object has no attribute 'k'
Traceback (most recent call last):
  File "D:\Anaconda3\envs\tuneta\lib\site-packages\IPython\core\formatters.py", line 224, in catch_format_error
    r = method(self, *args, **kwargs)
  File "D:\Anaconda3\envs\tuneta\lib\site-packages\IPython\core\formatters.py", line 702, in __call__
    printer.pretty(obj)
  File "D:\Anaconda3\envs\tuneta\lib\site-packages\IPython\lib\pretty.py", line 394, in pretty
    return _repr_pprint(obj, self, cycle)
  File "D:\Anaconda3\envs\tuneta\lib\site-packages\IPython\lib\pretty.py", line 700, in _repr_pprint
    output = repr(obj)
  File "D:\Anaconda3\envs\tuneta\lib\site-packages\sklearn\base.py", line 274, in __repr__
    repr_ = pp.pformat(self)
  File "D:\Anaconda3\envs\tuneta\lib\pprint.py", line 153, in pformat
    self._format(object, sio, 0, 0, {}, 0)
  File "D:\Anaconda3\envs\tuneta\lib\pprint.py", line 170, in _format
    rep = self._repr(object, context, level)
  File "D:\Anaconda3\envs\tuneta\lib\pprint.py", line 404, in _repr
    repr, readable, recursive = self.format(object, context.copy(),
  File "D:\Anaconda3\envs\tuneta\lib\site-packages\sklearn\utils\_pprint.py", line 189, in format
    return _safe_repr(
  File "D:\Anaconda3\envs\tuneta\lib\site-packages\sklearn\utils\_pprint.py", line 440, in _safe_repr
    params = _changed_params(object)
  File "D:\Anaconda3\envs\tuneta\lib\site-packages\sklearn\utils\_pprint.py", line 93, in _changed_params
    params = estimator.get_params(deep=False)
  File "C:\Users\john\AppData\Roaming\Python\Python38\site-packages\yellowbrick\base.py", line 342, in get_params
    params = super(ModelVisualizer, self).get_params(deep=deep)
  File "D:\Anaconda3\envs\tuneta\lib\site-packages\sklearn\base.py", line 205, in get_params
    value = getattr(self, key)
  File "C:\Users\john\AppData\Roaming\Python\Python38\site-packages\yellowbrick\utils\wrapper.py", line 42, in __getattr__
    return getattr(self._wrapped, attr)
AttributeError: 'KMeans' object has no attribute 'k'

Versions:

Python 3.8
scikit-learn==1.0
yellowbrick==1.3.post1
bbengfort commented 3 years ago

@jmrichardson I'm sorry we accidentally left your note get buried in our email. But thank you for submitting a bug report to Yellowbrick! The KMeans estimator does in fact use n_clusters as its attribute, not k but k should be accessed by the visualizer itself. Something has gone wrong here, and I think it might be related to version bump to scikit-learn 1.0. We haven't really looked into the new scikit-learn version yet, but I suspect this will be part of a lot of changes we'll need to make to get compatibility with the new version.

pkaf commented 2 years ago

I can not reproduce the error in an environment with

scikit-learn==1.0 python=3.8.8 macosx_10_13_x86_64

Screen Shot 2022-01-12 at 1 30 20 pm

bbengfort commented 2 years ago

Glad to have more information!

mrtrkmn commented 2 years ago

I suspect this kind of reported problems due to having different versions of required libraries.

Just another feedback, no problem on following environment:

Python 3.7.12
scikit-learn==1.0.2
yellowbrick==1.4
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.5 LTS
Release:    18.04
Codename:   bionic

Screen Shot 2022-02-20 at 08 39 27

lwgray commented 2 years ago

@jmrichardson As @bbengfort and @mrtrkmn mentioned, this is more than likely a dependency issue. Thank you for the original bug report. V1.4 of Yellowbrick was recently release, please upgrade pip install -U yellowbrick