question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pkafcommented, Jan 12, 2022

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

1reaction
bbengfortcommented, Nov 10, 2021

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AttributeError: 'KMeans' object has no attribute 'k'
Based on this, yellowbrick got updated to v1.5 , and the problem with AttributeError: 'KMeans' object has no attribute 'k' got solved.
Read more >
[FIXED] 'KMeans' object has no attribute 'k' - PythonFixing
AttributeError : 'KMeans' object has no attribute 'k'. The way I am generating the curve is as follows: from sklearn.cluster import KMeans ...
Read more >
AttributeError: 'KMeans' object has no attribute '_n_threads'
i have saved my kmeans clustering model using pickle and when i try to predict clusters on new data after loading it throws...
Read more >
'KMeans' object has no attribute 'labels_' with pytorch ... - GitHub
first of all I thank , I tried to train model with pytorch but I got the following error: AttributeError: 'KMeans' object has...
Read more >
AttributeError: 'KMeans' object has no attribute '_n_threads'
Hello, I created a KMeans model on some dataset, pickled it and then loaded it in the Streamlit app that I made.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found