Python 混淆矩阵错误';列表';对象没有属性';argmax';

Python 混淆矩阵错误';列表';对象没有属性';argmax';,python,tensorflow,machine-learning,keras,classification,Python,Tensorflow,Machine Learning,Keras,Classification,我正在为DCNN模型编写分类报告,但我面临一个错误。我的代码是 from sklearn.metrics import confusion_matrix test = ImageDataGenerator() test_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255) test_data = test_generator.flow_from_directory(directory="

我正在为DCNN模型编写分类报告,但我面临一个错误。我的代码是

from sklearn.metrics import confusion_matrix

test = 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' object has no attribute 'argmax'

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


虽然这里的诊断很简单,但以后请发布完整的错误跟踪。还请注意,错误之后出现的任何代码都与问题无关(从未执行),因此不应包含在此处,因为它只会造成不必要的混乱(已编辑)。pred,
预测从何而来?谢谢。然而,y_pred_binary=np.argmax(预测,轴=1)我面临另一个错误,那就是AxisError:轴1超出维度为1的数组的界限@desertnaut@TurjoyAhmed只有当您的
预测
由一个元组组成时才会发生这种情况,即
预测=[p0,p1]
(实际上没有轴1);但在这种情况下,混淆矩阵是没有意义的。请更新您的帖子以准确显示
预测
变量是什么。前一行中的
preds
是什么?那里的命令应该返回您实际查找的内容(假设
preds
确实是您的预测),而不是您似乎认为的索引。@TurjoyAhmed接受答案,如果它解决了您的问题。
predictions = [[0.1, 0.9], [0.8, 0.2]] # dummy data
y_pred_binary = predictions.argmax(axis=1)
# AttributeError: 'list' object has no attribute 'argmax'

# Use Numpy:
import numpy as np
y_pred_binary = np.argmax(predictions, axis=1)
y_pred_binary
# array([1, 0])