【问题标题】:'KMeans' object has no attribute 'k'“KMeans”对象没有属性“k”
【发布时间】:2021-11-16 00:36:12
【问题描述】:

我正在使用 Yellowbrick 包绘制数据集的肘部曲线,以使用 KMeans 作为模型找到数据集的最佳聚类数。

我正在使用 Scikit-learn 的 KMeans 和 Yellowbrick 的 kelbow_visualizer 函数。

肘部曲线已生成,我可以读取肘部值,但之后会引发以下错误:

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

我生成曲线的方式如下:

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)

data_standardized 是我的数据集,RSEED 是一个值为 2 的常数。

我不确定是什么问题,因为肘部曲线是在引发错误之前生成的。

这是完整的错误信息:

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)

【问题讨论】:

  • 试试n_clusters而不是k

标签: python python-3.x scikit-learn


【解决方案1】:

我通过将肘部方法的结果存储到一个变量中解决了这个问题,如下所示:

elbow = elbow_method(data_standardized,10)

【讨论】:

  • 它尝试了同样的方法,但没有成功。你用的是什么版本?我在 1.30post1
【解决方案2】:

只需删除 return 并将 pass 放在 bow_method() 方法的最后一行。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2022-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-08
  • 2021-06-15
  • 2019-10-25
  • 2022-11-29
  • 2023-03-05
相关资源
最近更新 更多