python 如何解决YellowBrick SilhouetteVisualizer中的“不适合”错误?

lf5gs5x2  于 2023-01-01  发布在  Python
关注(0)|答案(2)|浏览(180)

我尝试使用YellowBrick中的SilhouetteVisualizer函数为kmeans聚类创建一个轮廓图,但是我一直收到下面的错误。当我对同一个数据应用KElbowVisualizer函数(在YellowBrick中)时,我没有遇到这个问题。我用于这两个函数的代码如下所示。感谢任何人能帮助解决这个问题,谢谢!

    • 弯头可视化工具代码**
kmeans_kwargs = {"init": "random", "n_init": 10, "max_iter": 300, "random_state": 101}
kmeans = KMeans(n_clusters = k, **kmeans_kwargs)
visualizer = KElbowVisualizer(kmeans, k = (2, 31))
visualizer.fit(X)
visualizer.show()
    • 轮廓可视化工具代码**
model = KMeans(n_clusters = 9)  #plotted for k = 9 based on optimal k from elbow visualizer above
visualizer = SilhouetteVisualizer(model)
visualizer.fit(X)         
visualizer.show()
    • 错误**
---------------------------------------------------------------------------
NotFittedError                            Traceback (most recent call last)
File ~/miniforge3/envs/tensorflow/lib/python3.9/site-packages/yellowbrick/utils/helpers.py:50, in is_fitted(estimator)
     49 try:
---> 50     estimator.predict(np.zeros((7, 3)))
     51 except sklearn.exceptions.NotFittedError:

File ~/miniforge3/envs/tensorflow/lib/python3.9/site-packages/sklearn/cluster/_kmeans.py:1019, in _BaseKMeans.predict(self, X, sample_weight)
    999 """Predict the closest cluster each sample in X belongs to.
   1000 
   1001 In the vector quantization literature, `cluster_centers_` is called
   (...)
   1017     Index of the cluster each sample belongs to.
   1018 """
-> 1019 check_is_fitted(self)
   1021 X = self._check_test_data(X)

File ~/miniforge3/envs/tensorflow/lib/python3.9/site-packages/sklearn/utils/validation.py:1345, in check_is_fitted(estimator, attributes, msg, all_or_any)
   1344 if not fitted:
-> 1345     raise NotFittedError(msg % {"name": type(estimator).__name__})

NotFittedError: This KMeans instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
Input In [87], in <cell line: 5>()
      3 model = KMeans(n_clusters = 9)
      4 vis = SilhouetteVisualizer(model)
----> 5 visualizer.fit(rfm_scaled)
      6 visualizer.show()

File ~/miniforge3/envs/tensorflow/lib/python3.9/site-packages/yellowbrick/cluster/silhouette.py:136, in SilhouetteVisualizer.fit(self, X, y, **kwargs)
    129 """
    130 Fits the model and generates the silhouette visualization.
    131 """
    132 # TODO: decide to use this method or the score method to draw.
    133 # NOTE: Probably this would be better in score, but the standard score
    134 # is a little different and I'm not sure how it's used.
--> 136 if not check_fitted(self.estimator, is_fitted_by=self.is_fitted):
    137     # Fit the wrapped estimator
    138     self.estimator.fit(X, y, **kwargs)
    140 # Get the properties of the dataset

File ~/miniforge3/envs/tensorflow/lib/python3.9/site-packages/yellowbrick/utils/helpers.py:116, in check_fitted(estimator, is_fitted_by, **kwargs)
     85 """
     86 Determines whether or not to check if the model has been fitted, and will return
     87 ``True`` if so. The ``is_fitted_by`` argument is set to ``'auto'`` by default,
   (...)
    113     Whether or not the model is already fitted
    114 """
    115 if isinstance(is_fitted_by, str) and is_fitted_by.lower() == "auto":
--> 116     return is_fitted(estimator)
    117 return bool(is_fitted_by)

File ~/miniforge3/envs/tensorflow/lib/python3.9/site-packages/yellowbrick/utils/helpers.py:51, in is_fitted(estimator)
     49 try:
     50     estimator.predict(np.zeros((7, 3)))
---> 51 except sklearn.exceptions.NotFittedError:
     52     return False
     53 except AttributeError:
     54     # Some clustering models (LDA, PCA, Agglomerative) don't implement ``predict``

AttributeError: module 'sklearn' has no attribute 'exceptions'
fcipmucu

fcipmucu1#

检查以确保使用的变量名正确
变更

vis = SilhouetteVisualizer(model)
visualizer.fit(X)         
visualizer.show()

visualizer = SilhouetteVisualizer(model)
visualizer.fit(X)         
visualizer.show()
83qze16e

83qze16e2#

你使用的是什么版本的yellowbrick和scikit-learn?你可能需要更新yellowbrick。我运行你的代码没有问题。

相关问题