How to format a confusion matrix to be in percentages summing up rows to 100%? Having the following code:
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
cm = confusion_matrix(y_true, y_pred, labels=["Up", "Down"])
disp = ConfusionMatrixDisplay(confusion_matrix=cm,display_labels=["Up", "Down"])
disp.plot(cmap="OrRd")
#disp.ax_.get_images()[0].set_clim(0, 1)
disp.ax_.set_title("Logistic Regression Vs Actual")
Tried adding normalize='pred'
:
cm = confusion_matrix(y_true, y_pred, labels=["Up", "Down"], normalize='pred')
But got summing up to 1:
while I need it in percentage.
CodePudding user response:
After the line : cm = confusion_matrix(y_true, y_pred, labels=["Up", "Down"])
in above code you add the following line :
cm = cm / cm.sum(axis = 0) *100
Please try it and let me know if it worked in comment
EDIT
Or you can do :
cm = confusion_matrix(y_true, y_pred, normalize = 'pred')
cm *= 100
CodePudding user response:
This is the solution I got:
cm = cm / cm.sum(axis=1)[:, np.newaxis]
and
disp.plot(cmap="OrRd", values_format=".2%")
The image is: