Learning outcomes
After this lesson, you will be able to:
- Explain the purpose of a confusion matrix and read its rows and columns correctly.
- Distinguish TP, TN, FP and FN using the actual class and the model prediction.
- Build a matrix from labelled classification results and verify its total.
- Interpret a false alarm and a missed event in the context of a practical task.
- Explain why a matrix describes error structure but does not, by itself, select the best model.
- Understand how the matrix extends to multiple classes and why label order matters.
1. From individual predictions to an error structure
A classifier receives an object and assigns it to one of a predefined set of classes. Once reliable labels are available, we can compare each prediction with the actual answer. A simple count of correct and incorrect predictions loses important information: which class was actually present, and in which direction did the model make a mistake?
A confusion matrix is a table that jointly displays actual classes and predicted classes. In binary classification it has four cells. Each cell answers a separate question: was the object positive or negative in reality, and what did the model predict?
A confusion matrix groups classification outcomes by pairs of actual and predicted classes, revealing not only how many mistakes occurred but also what type they were.
In this lesson, the positive class is the event we want to detect, such as a defective part or an emergency condition. The negative class means that the event is absent. Positive does not mean “good”, and negative does not mean “bad”; these are technical labels only.
2. The binary classification matrix
In the orientation used here, rows show the actual class, while columns show the model prediction. Find the row for the real condition first, then the column for the model's answer. Their intersection contains the number of objects with that combination.
| Actual \ Predicted | Positive | Negative |
|---|---|---|
| Positive | TPTrue PositiveEvent present; model detects it | FNFalse NegativeEvent present; model misses it |
| Negative | FPFalse PositiveNo event; model raises an alarm | TNTrue NegativeNo event; model correctly rejects the alarm |
The abbreviations combine two ideas. True means the prediction matches the actual class; False means it does not. Positive and Negative name the class predicted by the model.
3. Four outcomes in a practical example
Imagine an inspection system for manufactured parts. The positive class is a cracked part; the negative class is a part without a crack. After manual inspection of 200 parts, we obtain this matrix:
| Actual \ Predicted | Crack | No crack | Total |
|---|---|---|---|
| Crack | 18TP | 2FN | 20 |
| No crack | 12FP | 168TN | 180 |
| Total predictions | 30 | 170 | 200 |
TP = 18: the system detected eighteen parts that really had cracks. These are useful detections.
FN = 2: the system classified two cracked parts as sound. These are misses: a potentially defective part may continue through production.
FP = 12: the system flagged twelve sound parts as defective. These are false alarms: additional inspection, delays and unnecessary costs may follow.
TN = 168: the system correctly cleared 168 parts that had no cracks.
4. Why FP and FN are not interchangeable
Both cells represent mistakes, but their consequences can be very different. In the crack-inspection example, an FN is a missed defect that may cause product failure. An FP is a false detection that increases inspection workload and cost. Which risk matters more depends on the system's purpose, not on the cell's name.
Consider other settings:
- Medical screening: an FN may mean that a potential disease case is not referred for further examination; an FP may lead to an unnecessary check for a healthy person.
- Spam filtering: an FP may send an important message to spam; an FN may allow an advertising message into the inbox.
- Fraud detection: an FP may delay a legitimate transaction; an FN may allow a fraudulent transaction to pass.
One error type is not always worse. Return to the evaluation brief from lesson 1.5.1: identify who is affected, the consequences, whether the decision can be reversed, and whether a person reviews an alert.
FP means the model reports a positive event that is not actually present—a false alarm. FN means the model reports no event even though it exists—a miss.
5. How to build a matrix without swapping the axes
To build a matrix, you need pairs of actual labels and predictions for the same set of objects. Make sure each prediction is matched to the correct object and both sequences use the same class encoding.
- Choose an evaluation set that was not used to fit the model.
- Specify which label is the positive class.
- For each object, compare its actual label with the prediction.
- Increment the corresponding TP, TN, FP or FN cell.
- Check that the four cells sum to the number of objects, with no omissions or duplicate counting.
- Record the row and column order alongside the result.
Libraries may let you set label order explicitly, and plotting tools may use different orientations. This lesson puts actual classes in rows and predictions in columns. When reading a matrix from another tool, check its axis labels first; otherwise FP and FN can be swapped.
6. Binary and multiclass matrices
For a task with three or more classes, the matrix becomes square: each row represents an actual class and each column a predicted class. Values on the main diagonal are correct classifications; values off the diagonal are mistakes. The position of an off-diagonal cell shows which two classes the model confused.
For example, an image classifier may distinguish cats, dogs and birds. The error “cat → dog” differs from “bird → cat”. A single total of mistakes does not show which pairs are confused. The matrix helps locate these patterns and decide whether more data, better features or clearer labels are needed.
For a multiclass task, TP/FP/FN/TN can be defined for one class against all the others: treat the selected class as positive and combine the remaining classes into negative. This is called one-vs-rest. Per-class metrics and averaging strategies will be covered later.
7. Limitations and common reading mistakes
- Swapped axes: without reading the headings, FP and FN can be reversed. Always check which axis is actual and which is predicted.
- Unspecified positive class: the meaning of TP/FP/FN/TN depends on which event is positive. State it explicitly.
- Different evaluation sets: matrices of the same size are not necessarily comparable if the data and protocol differ.
- Incorrect labels: a matrix treats reference labels as ground truth, but annotations may also be wrong.
- No cost context: the matrix counts outcomes but does not tell you the harm they cause.
- No universal score: a matrix describes observed outcomes; it is not a universal measure of suitability.
A matrix also does not show how confident the model was if it receives only final class predictions. Analysing thresholds and ranking requires probability estimates or scores; we will return to them in later lessons.
Practice: analyse a classifier's results
Use the matrix for the 200 inspected parts. Write down your answers:
- Name the positive and negative classes.
- Explain in your own words what the 18 in TP and the 2 in FN mean.
- Describe the practical consequence of the 12 false alarms in FP.
- Check the sum of the four cells and explain why it must equal the evaluation-set size.
- Give one situation where missing an FN is more costly than an FP, and one where an FP may be more costly.
- Draw a three-class matrix and mark its correct-prediction diagonal and one pair of confused classes.
Completion criterion: you identify all four outcomes correctly, do not confuse actual labels with predictions, and connect FP/FN to the consequences of a specific task. You do not need to calculate Accuracy, Precision, Recall or F1 yet; those measures are the subject of the next lesson.
Lesson summary
A confusion matrix shows how actual classes relate to model predictions. In binary classification, TP and TN are correct outcomes, FP is a false alarm, and FN is a miss. Their meaning depends on the selected positive class and the orientation of the axes. The matrix reveals error structure, helps verify that every object was counted, and supports discussion of consequences—but it does not replace task context or evaluation metrics. In the next lesson, we will turn these four counts into Accuracy, Precision, Recall and F1, and learn how to choose a measure for a task.
Sources and further reading
Lesson glossary
A table of actual and predicted classes that counts each type of classification outcome.
An object truly belongs to the positive class, and the model correctly predicts that class.
An object truly belongs to the negative class, and the model correctly predicts the negative class.
The model predicts a positive class for an object that actually belongs to the negative class; a false alarm.
The model predicts a negative class for an object that actually belongs to the positive class; a miss.
Knowledge check
Select an answer and press “Check answer”. Feedback appears after checking.
Question 1
What is the primary purpose of a confusion matrix?
Question 2
What does TP mean?
Question 3
Which outcome is a false alarm?
Question 4
The system fails to detect a real defect. Which cell contains this object?
Question 5
What is the sum of the four inner cells in the parts example?
Question 6
How are the axes arranged in this lesson's matrices?
Question 7
True or false: a confusion matrix alone always tells us which model is better for every application.