Scikit-Learn的AttributeError 'list'对象没有属性'shape',即使我已经验证了输入是一个numpy数组

mcdcgff0  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(94)

运行此函数时

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 Dataframe 的values属性。
我使用print语句来验证.predict的输入数据是一个numpy数组,甚至得到了data.shape的值,但我似乎无法弄清楚为什么这个错误仍然存在。

tjjdgumg

tjjdgumg1#

我没有看到你的类的方法,但是Silhouette reference是这样定义的:

sklearn.metrics.silhouette_score(X, labels)

因此,第一个条件看起来不错,但第二个条件包括

score = silhouette_score(data, predicted_scores)

把分数放在这里很奇怪,考虑用标签代替它。
Hope这有帮助

相关问题