Confusion Matrix

What is a Confusion Matrix?

A confusion matrix is a fundamental tool in the field of machine learning and classification. It is used to evaluate the performance of a classification model by comparing its predictions with the actual labels. This matrix provides a clear and concise summary of the model’s performance, helping data scientists and machine learning practitioners understand where their model excels and where it falls short.

Creating a Confusion Matrix

To create a confusion matrix, you first need a classification model and a dataset with known labels. Suppose you have a binary classification problem, where you are predicting whether an email is spam or not. The matrix would look like this:

from sklearn.metrics import confusion_matrix

# True labels
true_labels = [1, 0, 1, 0, 1, 1, 0, 0]

# Predicted labels
predicted_labels = [1, 0, 1, 1, 1, 0, 0, 1]

confusion = confusion_matrix(true_labels, predicted_labels)

Results Explained

Once you have your confusion matrix, it’s time to interpret the results. There are four essential components:

  • True Positives (TP): The model correctly predicted positive instances.
  • True Negatives (TN): The model correctly predicted negative instances.
  • False Positives (FP): The model incorrectly predicted positive instances (Type I error).
  • False Negatives (FN): The model incorrectly predicted negative instances (Type II error).

Created Metrics

Based on the confusion matrix, we can calculate several essential metrics:

Sensitivity (Recall)

Sensitivity, also known as recall, measures the model’s ability to correctly identify positive instances. It is calculated as:


A high sensitivity value indicates that the model rarely misses positive instances.


Specificity measures the model’s ability to correctly identify negative instances. It is calculated as:


A high specificity value indicates that the model rarely misclassifies negative instances.


The F-score is the harmonic mean of precision and recall (sensitivity). It provides a balanced measure of a model’s performance, especially when dealing with imbalanced datasets:



Understanding and effectively using a confusion matrix is crucial for evaluating the performance of machine learning models. By creating a confusion matrix and calculating metrics like sensitivity, specificity, and F-score, you gain valuable insights into how well your model is performing. These insights empower you to make informed decisions and improve your model’s accuracy.

Incorporate confusion matrices into your machine learning workflow to become a more proficient data scientist and make better-informed decisions when building classification models.