在 Python SKLEARN 中保存混淆矩阵

Jam*_*usk 2 python numpy matplotlib confusion-matrix scikit-learn

我正在为 6 个不同的训练数据值生成 6 个不同的混淆矩阵,并且我正在尝试将生成的混淆矩阵保存为图像。不幸的是,当他们保存时,他们一直保存为空白的 jpeg 图像;但是,当我使用 show() 来显示它们时,它们是可见的。这是我的代码

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')
Run Code Online (Sandbox Code Playgroud)

我正在使用以下库:

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
Run Code Online (Sandbox Code Playgroud)

如何有效地保存混淆矩阵?

Jam*_*usk 5

我解决了我遇到的问题。如果有人想知道,我修改了代码并解决了这个问题。

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')
Run Code Online (Sandbox Code Playgroud)