Save Confusion Matrix in Python SKLEARN

12,925

I fixed the problem I was having. In case anyone is wondering, I modified the code to this and it resolved the problem.

for matrix in confusion_matrices:
    fig = plt.figure()
    plt.matshow(cm)
    plt.title('Problem 1: Confusion Matrix Digit Recognition')
    plt.colorbar()
    plt.ylabel('True Label')
    plt.xlabel('Predicated Label')
    plt.savefig('confusion_matrix'+str(learning_values.pop())+'.jpg')
Share:
12,925

Related videos on Youtube

James Musk
Author by

James Musk

Updated on June 04, 2022

Comments

  • James Musk
    James Musk almost 2 years

    I'm generating 6 different confusion matrices for 6 different values of training data and I'm trying to save the generated confusion matrices as images. Unfortunately, when they save they keep saving as blank jpeg images; however, when I use show() to display them they are visible. Here is my code

    for matrix in confusion_matrices:
            fig = plt.figure()
            plt.matshow(cm)
            plt.title('Problem 1: Confusion Matrix Digit Recognition')
            plt.colorbar()
            plt.ylabel('True Label')
            plt.xlabel('Predicated Label')
            fig.savefig('confusion_matrix'+str(learning_values.pop())+'.jpg')
    

    I'm using the following libraries:

    import matplotlib.pyplot as plt
    import numpy
    from numpy import ravel, reshape, swapaxes
    import scipy.io
    from sklearn import svm
    from sklearn.metrics import confusion_matrix
    from random import sample
    

    How do I efficiently save confusion matrices?

    • Warren Weckesser
      Warren Weckesser over 8 years
      Where is saveas defined? Just a guess: whatever saveas is, perhaps it requires that plt.show() has been called before saveas is called. Have you tried plt.savefig(...)?