运行此功能时
def evaluate_performance(self, data):
data = np.array(data)
print(data.shape[0])
if self.cluster_method == 'kmeans':
predicted_labels = self.cluster_model.predict(data)
score = silhouette_score(data, predicted_labels)
print(f"Silhouette score: {score}")
elif self.cluster_method == 'isolation_forest':
predicted_scores = self.cluster_model.score_samples(data)
score = silhouette_score(data, predicted_scores)
print(f"Silhouette score: {score}")
我收到这个错误
AttributeError: 'list' object has no attribute 'shape'
在一个单独的函数中从 csv 文件访问数据,该函数返回 pandas 数据框的值属性。
我使用打印语句来验证
.predict
的输入数据是一个numpy数组,甚至得到了data.shape
的值,但我似乎无法弄清楚为什么这个错误仍然存在。
我没有看到你班级的方法,但是Silhouette参考是这样定义的:
sklearn.metrics.silhouette_score(X, labels)
因此你的第一个条件看起来不错,但第二个条件包括
score = silhouette_score(data, predicted_scores)
把分数放在这里很奇怪,考虑用标签代替它。
希望有帮助