Ammar Kamran
Ammar Kamran

Reputation: 139

How to interpret the Confusion Matrix in Python for 2 classes

I am implementing a machine learning model in Python which predicts success or failure. I have created a dummy variable which is 1 when there is success and 0 when there is a failure. I understand the concept of confusion matrix but I have found some online where the TPs and TNs are on opposite sides of the matrix. I would like to know how to interpret the results for my variables. Is the top-left corner of the matrix predicting True Positive? If so would that translate to the amount of successes being predicted correctly or the amount of failures being predicted correctly?

enter image description here

Does the matrix match the diagram below and if so how?

enter image description here

Ideally, please describe each corner of the confusion matrix in the context where I have success as 1 and failure as 0.

Upvotes: 0

Views: 1930

Answers (2)

Sharon Choong
Sharon Choong

Reputation: 596

Refer to the documentation: https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html

Since you haven't specified the third parameter for labels in confusion_matrix, the labels in y_test_res will be used in sorted order, i.e. in this case 0 then 1. The row labels represent actual y, and column labels represent predicted y.

So the top-left corner is showing the number of failure observations, i.e. the actual y was 0 and was predicted 0, i.e. true negatives. The bottom-right corner is showing true positives, i.e. the actual y was 1 and was predicted 0.

The top-right corner would be actual y = 0 and predicted y = 1, i.e. false positive.

Using the confusion matrix plot would prettify things a little.

from sklearn.metrics import plot_confusion_matrix
plot_confusion_matrix(forest, X_test, y_test)  
print(plt.show())

Upvotes: 2

Ismael EL ATIFI
Ismael EL ATIFI

Reputation: 2118

In the case of binary classification where classes are 0 and 1 and according to the doc :

  • 1st row is for class 0
  • 2nd row is for class 1
  • 1st column is for predicted class 0
  • 2nd column is for predicted class 1

Coefficient (0, 0) is the True Negative count (TN).
Coefficient (0, 1) is the False Positive count (FP).
Coefficient (1, 0) is the False Negative count (FN).
Coefficient (1, 1) is the True Positive count (TP).

Upvotes: 1

Related Questions