0
$\begingroup$

ConfusionMatrixDisplay docs say:

It is recommend to use from_estimator or from_predictions to create a ConfusionMatrixDisplay.

However, I want to plot the matrix manually on some axes I configure, and when I use from_predictions, I can't prevent it from plotting the matrix. The result is that I get two plots shown: one from the from_predictions call, and one from cmp.plot().

For example, this results in two plots:

import numpy as np
from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix
import matplotlib.pyplot as plt

cmp = ConfusionMatrixDisplay.from_predictions(np.arange(25), np.arange(25))
fig, ax = plt.subplots(figsize=(10,10))
cmp.plot(ax=ax)

enter image description here

But this results in one:

import numpy as np
from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix
import matplotlib.pyplot as plt

cm = confusion_matrix(np.arange(25), np.arange(25))
cmp = ConfusionMatrixDisplay(cm, display_labels=np.arange(25))
fig, ax = plt.subplots(figsize=(10,10))
cmp.plot(ax=ax)

enter image description here

I would like to use from_predictions, but to have it not plot, like using the constructor.

Notebook: https://drive.google.com/file/d/1lUPpp0dYfuIk1z8o7QfdUlQTt9eJ1dy4/view?usp=sharing

$\endgroup$

1 Answer 1

2
$\begingroup$

The from_predictions has an extra keyword argument that you can use to plot the figure on an existing Axes object (see also the documentation, which you can use to replace the cmp.plot(ax=ax) call. The following should therefore do what you're looking for:

fig, ax = plt.subplots(figsize=(10, 10))
cmp = ConfusionMatrixDisplay.from_predictions(np.arange(25), np.arange(25), ax=ax)
$\endgroup$
1
  • $\begingroup$ Yes, thank you, missed that somehow $\endgroup$ Jun 14 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.