Python Tutorials · Machine Learning

Confusion Matrix

Learn all about Confusion Matrix in this comprehensive tutorial.

5 min read advanced
  • It is a table that is used in classification problems to assess where errors in the model were made.
  • Confusion matrixes can be created by predictions made from a logistic regression.
  • The Confusion Matrix created has four different quadrants:
  • The matrix provides us with many useful metrics that help us to evaluate our classification model.
  • Accuracy measures how often the model is correct.
  • Of the positives predicted, what percentage is truly positive?
  • Of all the positive cases, what percentage are predicted positive?
  • How well the model is at prediciting negative results?
  • F-score is the "harmonic mean" of precision and sensitivity.

What is a confusion matrix?

It is a table that is used in classification problems to assess where errors in the model were made.

The rows represent the actual classes the outcomes should have been. While the columns represent the predictions we have made. Using this table it is easy to see which predictions are wrong.

Creating a Confusion Matrix

Confusion matrixes can be created by predictions made from a logistic regression.

For now we will generate actual and predicted values by utilizing NumPy:

Next we will need to generate the numbers for "actual" and "predicted" values.

In order to create the confusion matrix we need to import metrics from the sklearn module.

Once metrics is imported we can use the confusion matrix function on our actual and predicted values.

To create a more interpretable visual display we need to convert the table into a confusion matrix display.

Vizualizing the display requires that we import pyplot from matplotlib.

Finally to display the plot we can use the functions plot() and show() from pyplot.

See the whole example in action:

python

Results Explained

The Confusion Matrix created has four different quadrants:

True Negative (Top-Left Quadrant) False Positive (Top-Right Quadrant) False Negative (Bottom-Left Quadrant) True Positive (Bottom-Right Quadrant)

True means that the values were accurately predicted, False means that there was an error or wrong prediction.

Now that we have made a Confusion Matrix, we can calculate different measures to quantify the quality of the model. First, lets look at Accuracy.

Created Metrics

The matrix provides us with many useful metrics that help us to evaluate our classification model.

The different measures include: Accuracy, Precision, Sensitivity (Recall), Specificity, and the F-score, explained below.

Accuracy

Accuracy measures how often the model is correct.

(True Positive + True Negative) / Total Predictions

python

Precision

Of the positives predicted, what percentage is truly positive?

True Positive / (True Positive + False Positive)

Precision does not evaluate the correctly predicted negative cases:

python

Sensitivity (Recall)

Of all the positive cases, what percentage are predicted positive?

Sensitivity (sometimes called Recall) measures how good the model is at predicting positives.

This means it looks at true positives and false negatives (which are positives that have been incorrectly predicted as negative).

True Positive / (True Positive + False Negative)

Sensitivity is good at understanding how well the model predicts something is positive:

python

Specificity

How well the model is at prediciting negative results?

Specificity is similar to sensitivity, but looks at it from the perspective of negative results.

True Negative / (True Negative + False Positive)

Since it is just the opposite of Recall, we use the recall_score function, taking the opposite position label:

python

F-score

F-score is the "harmonic mean" of precision and sensitivity.

It considers both false positive and false negative cases and is good for imbalanced datasets.

2 * ((Precision * Sensitivity) / (Precision + Sensitivity))

This score does not take into consideration the True Negative values:

python

All calulations in one:

python

Module quiz

2 questions
1

Which of the following is true about Confusion Matrix?

2

What is the most common pitfall when working with Confusion Matrix?

Answer all questions to submit.