A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Confusion Matrix and Classification Metrics

Graduate Depth 83 in the knowledge graph ☐ I know this ☆ Set as goal
2topics build on this
399prerequisites beneath it
See this on the map →
Supervised Learning FundamentalsProbability Axioms→→Classification Metrics and Evaluation
confusion-matrix tp fp tn fn

Core Idea

A confusion matrix tabulates true positives, false positives, true negatives, and false negatives for binary classification (generalizes to multi-class). It enables computing precision, recall, specificity, and F1-score. Visualizing confusion matrices reveals which classes are confused, guiding targeted model improvements.

Explainer

From supervised learning, you know that a classifier is trained on labeled examples and then evaluated on held-out data. But "how accurate is my model?" turns out to be a dangerously simplistic question. If 95% of emails are not spam, a model that always predicts "not spam" achieves 95% accuracy while being completely useless at its actual job. The confusion matrix replaces this single number with a complete picture of what your classifier gets right and wrong, broken down by class.

For binary classification, the confusion matrix is a 2×2 table. One axis represents the actual class (positive or negative), the other represents the predicted class. This creates four cells: true positives (TP) — correctly identified positives; false positives (FP) — negatives incorrectly called positive; true negatives (TN) — correctly identified negatives; and false negatives (FN) — positives incorrectly called negative. In a medical screening example, a TP is a sick patient correctly diagnosed, an FP is a healthy patient incorrectly flagged (a false alarm), a TN is a healthy patient correctly cleared, and an FN is a sick patient missed by the test. Each type of error has different real-world costs, and the confusion matrix forces you to confront them separately.

From these four numbers, you can derive every standard classification metric. Precision = TP/(TP+FP) answers "of everything the model called positive, how many actually were?" Recall (or sensitivity) = TP/(TP+FN) answers "of all actual positives, how many did the model catch?" Specificity = TN/(TN+FP) answers the same question for negatives. The F1-score = 2·(precision·recall)/(precision+recall) is the harmonic mean of precision and recall, useful when you want a single number that balances both. The key insight is that precision and recall trade off against each other: making a model more aggressive (predicting positive more often) increases recall but decreases precision, and vice versa. The confusion matrix makes this tradeoff visible and quantifiable.

For multi-class problems, the confusion matrix extends to an N×N table where entry (i, j) counts how many examples of class i were predicted as class j. The diagonal contains correct predictions; off-diagonal entries reveal specific confusions. If a digit recognizer frequently puts "7" in the "1" column, you know exactly which pair of classes needs attention — perhaps adding training examples that emphasize the crossbar of the 7, or engineering features that distinguish vertical strokes from angled ones. This diagnostic power is why the confusion matrix is the first thing experienced practitioners examine after training a classifier, long before looking at any aggregate metric.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes → Objects and Instances → Methods and Attributes → Algorithm Design Basics → Supervised Learning Fundamentals → Confusion Matrix and Classification Metrics

Longest path: 84 steps · 399 total prerequisite topics

Prerequisites (2)

Leads To (1)