Why Does My Sklearn.metrics Confusion_matrix Output Look Transposed?
It's my understanding that confusion matrices should show the TRUE classes in the columns and the PREDICTED classes in the rows. Therefore the sum of the columns should be equal to
Solution 1:
The confusion probably arises because sklearn
follows a different convention for axes of confusion matrix than the wikipedia article. So, to answer your question: It gives you the output in that specific format because sklearn
expects you to read it in a specific way.
Here are the two different ways of writing confusion matrix:
- sklearn's way of reading/writing confusion matrix: true labels in rows, and predicted labels in columns
- wikipedia example opposite of
sklearn
Solution 2:
Solution 3:
It is possible to do as you wish using sklearn, only change the code below appropriately
from sklearn.metricsimportConfusionMatrixDisplayimport matplotlib.pyplotas plt
fig, ax = plt.subplots(1,1,figsize=(7,4))
ConfusionMatrixDisplay(confusion_matrix(predict,y_test,labels=[1,0]),
display_labels=[1,0]).plot(values_format=".0f",ax=ax)
ax.set_xlabel("True Label")
ax.set_ylabel("Predicted Label")
plt.show()
Post a Comment for "Why Does My Sklearn.metrics Confusion_matrix Output Look Transposed?"