Skip to content Skip to sidebar Skip to footer

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:

Solution 2:

scikit-learn's confusion matrix follows a specific order and structure.

Reference: https://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html#sphx-glr-auto-examples-model-selection-plot-confusion-matrix-py

enter image description here

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?"