confusion_matrix 错误 ‘list’ 对象没有属性 ‘argmax’

我在为 DCNN 模型编写分类报告时遇到了一个错误。我的代码是

from sklearn.metrics import confusion_matrixtest = ImageDataGenerator()test_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)test_data = test_generator.flow_from_directory(directory="/content/dataset/test",target_size=IMAGE_SHAPE , color_mode="rgb" , class_mode='categorical' , batch_size=1 , shuffle = False )test_data.reset()predicted_class_indices=np.argmax(pred,axis=1)cm = confusion_matrix(test_labels, predictions.argmax(axis=1))

错误:

AttributeError: 'list' 对象没有属性 'argmax'

回答:

显然,您的 predictions 是一个 Python 列表,而列表没有 argmax 属性;您需要使用 Numpy 函数 argmax() :

predictions = [[0.1, 0.9], [0.8, 0.2]] # 示例数据y_pred_binary = predictions.argmax(axis=1)# AttributeError: 'list' 对象没有属性 'argmax'# 使用 Numpy:import numpy as npy_pred_binary = np.argmax(predictions, axis=1)y_pred_binary# array([1, 0])

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注