Lesson Introduction

Classification metrics help us evaluate model performance on classification tasks like predicting spam emails or diagnosing diseases. They help us determine if our model performs well.

By the end, you'll understand:

  • Confusion Matrix and its interpretation.
  • Accuracy, Precision, and Recall.
  • F1-score
  • How to compute these metrics using Python and SciKit Learn.

Let's dive in!

Confusion Matrix

A Confusion Matrix describes the performance of a classification model. In the context of a confusion matrix, a positive prediction is predicting the class labeled 1, and a negative prediction is predicting the class labeled 0. The confusion matrix is a 2x2 table (for binary classification) that shows:

  • True Positives (TP): A number of the correct positive predictions.
  • True Negatives (TN): A number of the correct negative predictions.
  • False Positives (FP): A number of the incorrect positive predictions.
  • False Negatives (FN): A number of the incorrect negative predictions.

Imagine that you need to classify emails as spam (1) or not-spam (0). Let's define an example of predictions and then create our confusion matrix:

import numpy as np
from sklearn.metrics import confusion_matrix

# Sample classification dataset
y_true = np.array([0, 1, 0, 1, 0, 1, 1, 0, 1, 0])  # True labels
y_pred = np.array([1, 1, 1, 1, 0, 0, 1, 0, 1, 0])  # Predicted labels

# Calculating confusion matrix
conf_matrix = confusion_matrix(y_true, y_pred)
print(f"Confusion Matrix:\n{conf_matrix}")

Output:

Confusion Matrix:
[[3 2]
 [1 4]]

This tells us:

  • True Positives (TP): 4 (model correctly predicted spam four times)
  • True Negatives (TN): 3 (model correctly predicted not spam three times)
  • False Positives (FP): 2 (model incorrectly predicted spam two times)
  • False Negatives (FN): 1 (model incorrectly predicted not spam one time)

Note that the values in the confusion matrix are stored this way:

[[TN FP
  FN TP]]
What is Accuracy?
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal