2

I am using the Yellowbrick package to plot an elbow curve for a dataset to find the most optimal number of clusters for the dataset using KMeans as a model.

I am using Scikit-learn's KMeans and Yellowbrick's kelbow_visualizer functions.

The elbow curve is generated and I am able to read the elbow value however the following error is thrown afterwards:

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

The way I am generating the curve is as follows:

from sklearn.cluster import KMeans
from yellowbrick.cluster.elbow import kelbow_visualizer

def elbow_method(X, max_range_for_elbow, rseed = RSEED):
    return kelbow_visualizer(KMeans(random_state=rseed), X, k=(1, max_range_for_elbow)) 

elbow_method(data_standardized,10)

Where data_standardized is my dataset and RSEED is a constant with value 2.

I'm not sure what the issue is since the elbow curve is generated before the error is thrown.

This is the full error message:

AttributeError: 'KMeans' object has no attribute 'k'
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj, include, exclude)
    968 
    969             if method is not None:
--> 970                 return method(include=include, exclude=exclude)
    971             return None
    972         else:

~\anaconda3\lib\site-packages\sklearn\base.py in _repr_mimebundle_(self, **kwargs)
    462     def _repr_mimebundle_(self, **kwargs):
    463         """Mime bundle used by jupyter kernels to display estimator"""
--> 464         output = {"text/plain": repr(self)}
    465         if get_config()["display"] == 'diagram':
    466             output["text/html"] = estimator_html_repr(self)

~\anaconda3\lib\site-packages\sklearn\base.py in __repr__(self, N_CHAR_MAX)
    258             n_max_elements_to_show=N_MAX_ELEMENTS_TO_SHOW)
    259 
--> 260         repr_ = pp.pformat(self)
    261 
    262         # Use bruteforce ellipsis when there are a lot of non-blank characters

~\anaconda3\lib\pprint.py in pformat(self, object)
    151     def pformat(self, object):
    152         sio = _StringIO()
--> 153         self._format(object, sio, 0, 0, {}, 0)
    154         return sio.getvalue()
    155 

~\anaconda3\lib\pprint.py in _format(self, object, stream, indent, allowance, context, level)
    168             self._readable = False
    169             return
--> 170         rep = self._repr(object, context, level)
    171         max_width = self._width - indent - allowance
    172         if len(rep) > max_width:

~\anaconda3\lib\pprint.py in _repr(self, object, context, level)
    402 
    403     def _repr(self, object, context, level):
--> 404         repr, readable, recursive = self.format(object, context.copy(),
    405                                                 self._depth, level)
    406         if not readable:

~\anaconda3\lib\site-packages\sklearn\utils\_pprint.py in format(self, object, context, maxlevels, level)
    178 
    179     def format(self, object, context, maxlevels, level):
--> 180         return _safe_repr(object, context, maxlevels, level,
    181                           changed_only=self._changed_only)
    182 
~\anaconda3\lib\site-packages\sklearn\utils\_pprint.py in _safe_repr(object, context, maxlevels, level, changed_only)
    423         recursive = False
    424         if changed_only:
--> 425             params = _changed_params(object)
    426         else:
    427             params = object.get_params(deep=False)

~\anaconda3\lib\site-packages\sklearn\utils\_pprint.py in _changed_params(estimator)
     89     estimator with non-default values."""
     90 
---> 91     params = estimator.get_params(deep=False)
     92     init_func = getattr(estimator.__init__, 'deprecated_original',
     93                         estimator.__init__)

~\anaconda3\lib\site-packages\yellowbrick\base.py in get_params(self, deep)
    340         the estimator params.
    341         """
--> 342         params = super(ModelVisualizer, self).get_params(deep=deep)
    343         for param in list(params.keys()):
    344             if param.startswith("estimator__"):

~\anaconda3\lib\site-packages\sklearn\base.py in get_params(self, deep)
    193         out = dict()
    194         for key in self._get_param_names():
--> 195             value = getattr(self, key)
    196             if deep and hasattr(value, 'get_params'):
    197                 deep_items = value.get_params().items()

~\anaconda3\lib\site-packages\yellowbrick\utils\wrapper.py in __getattr__(self, attr)
     40     def __getattr__(self, attr):
     41         # proxy to the wrapped object
---> 42         return getattr(self._wrapped, attr)

Chops
  • 462
  • 2
  • 7
  • 19

2 Answers2

-1

I solved this issue by storing the result of the elbow_method function into a variable, as so:

elbow = elbow_method(data_standardized,10)
Chops
  • 462
  • 2
  • 7
  • 19
-1

just remove the return and put pass at last line of the elbow_method() method.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 23 '21 at 19:47