Changing values in confusion_matrix (sklearn)

Viewed 41

How can i change a numbers values to letters in both axis? I got dictionary with keys = letters and values = numbers

Code:

cm = confusion_matrix(y_class, y_pred_class)
disp = ConfusionMatrixDisplay(confusion_matrix=cm)
fig, ax = plt.subplots(figsize=(10,10))
ax.set_title("Confusion Matrix for Artificial neural network")
disp.plot(ax=ax)
plt.show()

confusion_matrix

Dict:

{
 'A': 7,'B': 6,'C': 14,'D': 2,'E': 19,'F': 13,
 'G': 4,'H': 15, 'I': 1, 'J': 8, 'K': 24, 'L': 17,
 'M': 9, 'N': 3, 'O': 11, 'P': 18 'Q': 22, 'R': 12,
 'S': 5, 'T': 0, 'U': 23, 'V': 20, 'W': 16, 'X': 10,
 'Y': 21, 'Z': 25
}
1 Answers

You can do this by setting the x and y axis labels with .set_ticklabels():

ax.xaxis.set_ticklabels([...])
ax.yaxis.set_ticklabels([...])

Where the list just contains the values you want. This will replace the default numbers of the matrix plot.

Related