Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting already calculated Confusion Matrix using Python [duplicate]

How can I plot in Python a Confusion Matrix similar do the one shown here for already given values of the Confusion Matrix?

In the code they use the method sklearn.metrics.plot_confusion_matrix which computes the Confusion Matrix based on the ground truth and the predictions.

But in my case, I already have calculated my Confusion Matrix. So for example, my Confusion Matrix is (values in percentages):

[[0.612, 0.388]
 [0.228, 0.772]]
like image 589
machinery Avatar asked Jul 16 '26 02:07

machinery


1 Answers

I saw that someone already answered this question, but I'm adding a new one that can be useful for the author or even for other users.

It is possible to plot in Python an already Confusion Matrix computed through mlxtend package:

Mlxtend (machine learning extensions) is a Python library of useful tools for the day-to-day data science tasks.

Snippet code:

# Imports
from mlxtend.plotting import plot_confusion_matrix
import matplotlib.pyplot as plt
import numpy as np

# Your Confusion Matrix
cm = np.array([[0.612, 0.388],
               [0.228, 0.772]])

# Classes
classes = ['class A', 'class B']

figure, ax = plot_confusion_matrix(conf_mat = cm,
                                   class_names = classes,
                                   show_absolute = False,
                                   show_normed = True,
                                   colorbar = True)

plt.show()

The output will be:

enter image description here

like image 138
whoisraibolt Avatar answered Jul 17 '26 16:07

whoisraibolt