Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Confusion Matrix in Python SKLEARN

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?

like image 490
James Musk Avatar asked Apr 11 '26 04:04

James Musk


1 Answers

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')
like image 137
James Musk Avatar answered Apr 12 '26 16:04

James Musk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!