如何解决YellowBrick SilhouetteVisualizer的‘Not Fitted’错误?
问题描述
尝试用YellowBrick的SilhouetteVisualizer绘制KMeans聚类的轮廓图时持续报错,但同一数据集使用KElbowVisualizer时正常。以下是代码及错误信息:
KElbowVisualizer 代码
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()
SilhouetteVisualizer 代码
model = KMeans(n_clusters = 9) # 根据肘部法则选择的最优k值 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'
问题原因
核心错误是AttributeError: module 'sklearn' has no attribute 'exceptions'——你的YellowBrick版本过旧,内部代码仍在调用sklearn.exceptions路径,但新版本Scikit-learn已调整异常类的存放路径,导致兼容性冲突。而KElbowVisualizer未触发该检查逻辑,因此未报错。
解决方法
方法1:升级YellowBrick到最新版本
执行以下命令更新YellowBrick,新版本已修复该兼容性问题:
pip install --upgrade yellowbrick
方法2:手动拟合KMeans后再传入可视化器
如果暂时无法升级库,可以先手动拟合KMeans模型,再传给SilhouetteVisualizer,跳过YellowBrick内部的拟合检查逻辑:
model = KMeans(n_clusters = 9) model.fit(X) # 先手动完成模型拟合 visualizer = SilhouetteVisualizer(model) visualizer.fit(X) # 可视化器将直接使用已拟合的模型 visualizer.show()
内容的提问来源于stack exchange,提问作者clpoh
相关产品推荐
相关产品推荐

