How to plot Confusion Matrix

Viewed 737

Please I would love some assistance to plot a confusion matrix from my model. Code displayed below:

    import os
    import glob
    from sklearn.model_selection import train_test_split
    import shutil
    from tensorflow.keras import callbacks
    from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
    from my_utils import create_generators
    from CNN_models import amazon_model
    import tensorflow as tf
    import matplotlib.pyplot as plt

if name=="main":

path_to_train = "data\\train"
path_to_val = "data\\val"
path_to_test = "data\\test"
batch_size = 128
epochs = 5
lr = 0.0001

train_generator, val_generator, test_generator = create_generators(batch_size, path_to_train, path_to_val, path_to_test)
nbr_classes = train_generator.num_classes

TRAIN=True
TEST=False

if TRAIN:
    path_to_save_model = './Models'
    ckpt_saver = ModelCheckpoint(
        path_to_save_model,
        monitor="val_accuracy",
        mode='max',
        save_best_only=True,
        save_freq='epoch',
        verbose=1
    )

    early_stop = EarlyStopping(monitor="val_accuracy", patience=5)

    tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir="./logs")

    csv_logger = tf.keras.callbacks.CSVLogger('first_model_training.log', separator=",", append=False)

    model = amazon_model(nbr_classes)
   
    optimizer = tf.keras.optimizers.Adam(learning_rate=lr, amsgrad=True)

    model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy', tf.keras.metrics.Precision(), tf.keras.metrics.Recall()])
    
    history = model.fit(train_generator,
                epochs=epochs,
                batch_size=batch_size,
                validation_data=val_generator,
                callbacks=[ckpt_saver, early_stop, tensorboard_callback, csv_logger]
            )

        
    acc = history.history['accuracy']
    print(acc)

    model.save("first_model.h5")

    from matplotlib.pyplot import figure
    figure(figsize=(8, 6))
    plt.plot(history.history['accuracy'])
    plt.plot(history.history['val_accuracy'])
    plt.title('model accuracy')
    plt.ylabel('accuracy')
    plt.xlabel('epoch')
    plt.legend(['train', 'val'], loc='upper left')
    plt.savefig('./plots/accuracy', dpi=200)
    plt.show()

    figure(figsize=(8, 6))
    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.savefig('./plots/loss', dpi=200)
    plt.show()

if TEST:
    model = tf.keras.models.load_model('./Models')
    model.summary()

    print("Evaluating validation set: ")
    model.evaluate(val_generator)

    print("Evaluating test set: ")
    model.evaluate(test_generator)

Sorry that it may be a bit of a newbie question but I would love to know what I need to add to the above code to make it plot a confusion matrix for my after it runs. I'm able to plot the graphs of both accuracy and loss for a few epochs, but I want to include Confusion Matrix before running for more epochs. Here are the plots already obtained:

accuracy plot

loss plot

1 Answers
  • Try this
  • The basic concept is to get prediction results from your model using X_test and then to compare these predictions to the real y_test results.
# 'Fake' and 'Real' are your dependent features for your classification use case, 
# where Fake == 0 and Real == 1. It is important that you use this form for the matrix.    

('Fake', 'Real') == (0, 1) 
('Fake', 'Real') == (0, 1) 
# Data handling
import pandas as pd

# Exploratory Data Analysis & Visualisation 
import matplotlib.pyplot as plt 
import seaborn as sns 

# Model improvement and Evaluation 
from sklearn import metrics 
from sklearn.metrics import confusion_matrix


# Plotting confusion matrix
matrix = pd.DataFrame((metrics.confusion_matrix(y_test, y_prediction)), 
                      ('Fake', 'Real'), 
                      ('Fake', 'Real'))
print(matrix)

# Visualising confusion matrix
plt.figure(figsize = (16,14),facecolor='white')
heatmap = sns.heatmap(matrix, annot = True, annot_kws = {'size': 20}, fmt = 'd', cmap = 'YlGnBu')
heatmap.yaxis.set_ticklabels(heatmap.yaxis.get_ticklabels(), rotation = 0, ha = 'right', fontsize = 18, weight='bold')
heatmap.xaxis.set_ticklabels(heatmap.xaxis.get_ticklabels(), rotation = 0, ha = 'right', fontsize = 18, weight='bold')

plt.title('Confusion Matrix\n', fontsize = 18, color = 'darkblue')
plt.ylabel('True label', fontsize = 14)
plt.xlabel('Predicted label', fontsize = 14)
plt.show()
  • If you need further help email us here: theanalyticsolutions@gmail.com
Related