无法打印正确的混淆矩阵,并且热图中的值也在示例 2e+2、e+4 等中打印

Posted

技术标签:

【中文标题】无法打印正确的混淆矩阵,并且热图中的值也在示例 2e+2、e+4 等中打印【英文标题】:Not able to print correct confusion matrix and also in heatmap values are printing in example 2e+2, e+4 etc etc 【发布时间】:2019-09-08 23:58:18 【问题描述】:

无法正确打印混淆矩阵,并且在打印热图中,某些块或列中的值正在打印,例如 2e+2、e+4 等。请帮助我

将 numpy 导入为 np 将 matplotlib.pyplot 导入为 plt 将 seaborn 导入为 sns 将熊猫导入为 pd 从 keras.models 导入顺序 从 keras.layers 导入 Convolution2D 从 keras.layers 导入 MaxPooling2D 从 keras.layers 导入 Flatten 从 keras.layers 导入密集 from sklearn.metrics 导入分类报告,混淆矩阵

from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

# Initialising the CNN
classifier = Sequential()

# Step 1 - Convolution
classifier.add(Convolution2D(64, 3, 3, input_shape = (64, 64, 3), activation = 'relu'))

# Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size = (2, 2)))

# Adding a second convolutional layer
classifier.add(Convolution2D(64, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))


classifier.add(Convolution2D(64, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))


# Step 3 - Flattening
classifier.add(Flatten())

# Step 4 - Full connection
classifier.add(Dense(output_dim = 128, activation = 'relu'))
classifier.add(Dense(output_dim = 10, activation = 'sigmoid'))

# Compiling the CNN
classifier.compile(optimizer = 'Adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

# Part 2 - Fitting the CNN to the images

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.4,
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

#importing training data

training_set = train_datagen.flow_from_directory('Dataset/train',
                                                 target_size = (64,64),
                                                 batch_size = 64,
                                                 class_mode = 'categorical')

#importing test data
test_set = test_datagen.flow_from_directory('Dataset/test',target_size = (64,64),
                                            batch_size = 64,
                                            class_mode = 'categorical',shuffle=False)

#storing all the history

history = classifier.fit_generator(
        training_set,
        steps_per_epoch=20,
        epochs=5,
        validation_data=test_set,
        validation_steps=2000)
print(history.history.keys())

#summarize 准确率

plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

#Confution Matrix 
Y_pred = classifier.predict_generator(test_set, steps=len(test_set), max_queue_size=10, workers=1, use_multiprocessing=False, verbose=0)
y_pred = np.argmax(Y_pred, axis=1)

#assigning values 
confusion=(confusion_matrix(test_set.classes, y_pred))
confusion_df = pd.DataFrame(confusion,
                     index = ['Airplan','Car','Birds','Cats','Deer', 'Dogs','Frog', 'Horse','Ship','Truck'], 
                     columns = ['Airplan','Car','Birds','Cats','Deer', 'Dogs','Frog', 'Horse','Ship','Truck'])

#heatmap    
sns.heatmap(confusion_df, annot=True)
print(confusion_df)

#classification report
print('Classification Report')
target_names = ['Airplan','Car','Birds','Cats','Deer', 'Dogs','Frog', 'Horse','Ship','Truck']
print(classification_report(test_set.classes, y_pred, target_names=target_names))

【问题讨论】:

“无法正确打印混淆矩阵”你能更好地解释一下吗?显示错误日志(如果有)。 @Anakin 混淆矩阵未正确打印意味着对角线元素未正确填充但完整的矩阵填充了数字。 请发布输出矩阵。另请解释实际结果与预期结果是什么。 @Hitsa 混淆矩阵表示系统正确识别特定类别的图像数量,即对角线应填充最大值,块应保留为 0 或 1 之类的值。但在代码中输出矩阵完全填充了不同的值....即对角线填充未突出显示。 【参考方案1】:

你能不能试试这个。

sns.heatmap(confusion_df, annot=True, fmt='.2f')

【讨论】:

【参考方案2】:

假设你的混淆矩阵是 cm

import numpy as np
import matplotlib.pyplot as plt
import seaborn  as  sns

cm = np.array([[345,12],[0,104763]])

plt.figure(figsize=(10,7))
sns.heatmap(cm,annot=True,linewidths=1, fmt = 'd')
plt.xlabel('predicted')
plt.ylabel('Truth')
plt.show()

你会得到下面的矩阵:

【讨论】:

以上是关于无法打印正确的混淆矩阵,并且热图中的值也在示例 2e+2、e+4 等中打印的主要内容,如果未能解决你的问题,请参考以下文章

热图值在Highcharts中萎缩

如何根据 GradientBoost 结果绘制热图?

使用python从混淆矩阵进行层次聚类

如何打印大维度的混淆矩阵

无法理解 SVM 返回的混淆矩阵

TensorBoard 中的 Tensorflow 混淆矩阵