Confusion Matrix Explained: Precision, Recall, F1 and Thresholds in Python
A confusion matrix shows exactly how a classifier is right and wrong: true positives, false positives, false negatives and true negatives. We built one by hand in Python for a fraud detector, derived accuracy, precision, recall, specificity and F1, showed why a model that never predicts fraud beats it on accuracy, and chose a threshold by business cost.
- A confusion matrix counts predictions in four cells: TP, FP, FN and TN; every common classification metric is built from them.
- Our fraud model caught 79 of 85 frauds (recall 0.929) but only 46 percent of its alerts were real (precision 0.459).
- A “model” that never predicts fraud scored 91.5 percent accuracy, beating the real model’s 90.1 percent while catching nothing.
- The best F1 came at threshold 0.6, but the lowest business cost was at 0.5, because missed fraud cost 25 times more than a false alarm.
“94 percent accurate” sounds impressive until you learn the model never catches the thing it was built to catch. A single number hides how a classifier fails, and different failures have very different costs: missing a fraudulent payment is not the same as flagging a genuine one. A confusion matrix lays out every kind of right and wrong answer in a small table, and from it you can derive the metric that actually matches your problem.
We computed everything from scratch with NumPy 2.5.5 on Python 3.13.6, using a synthetic, seeded dataset. scikit-learn’s confusion_matrix and classification_report produce the same numbers; it was not installed in our environment, so we did not use it.
What is a confusion matrix?
| Actual vs predicted | Predicted positive | Predicted negative |
|---|---|---|
| Actually positive | True positive (TP) | False negative (FN) |
| Actually negative | False positive (FP) | True negative (TN) |
Rows are what really happened and columns are what the model predicted (some tools swap them, so always check the labels). The diagonal holds the correct predictions. False positives are false alarms, also called Type I errors; false negatives are misses, Type II errors.
Our fraud example
rng = np.random.default_rng(21)
y = (rng.uniform(size=1000) < 0.08).astype(int) # about 8% fraud
score = np.where(y == 1, rng.normal(0.7, 0.18, 1000), # fraud scores higher on average
rng.normal(0.3, 0.15, 1000)).clip(0, 1)
# positives: 85 of 1000
1,000 transactions, 85 of them fraudulent, each with a model score between 0 and 1. Fraud tends to score high and genuine payments low, but the two overlap, as real model outputs do. The score could come from logistic regression, a random forest or any other classifier.
Building the confusion matrix in Python
def cm(y, p):
tp = int(((p == 1) & (y == 1)).sum()); tn = int(((p == 0) & (y == 0)).sum())
fp = int(((p == 1) & (y == 0)).sum()); fn = int(((p == 0) & (y == 1)).sum())
return tp, fp, fn, tn
p = (score >= 0.5).astype(int)
cm(y, p) # TP 79, FP 93, FN 6, TN 822
At the default threshold of 0.5, predictions are 1 (fraud) when the score is at least 0.5. Counting the four combinations of actual and predicted labels gives the matrix. With scikit-learn the equivalent is confusion_matrix(y, p).
| Threshold 0.5 | Predicted fraud | Predicted genuine |
|---|---|---|
| Actual fraud (85) | 79 (TP) | 6 (FN) |
| Actual genuine (915) | 93 (FP) | 822 (TN) |
Reading the four cells
The model caught 79 frauds and missed 6. It also raised 93 false alarms, genuine customers whose payments would be blocked or reviewed. Of the 172 alerts in total, fewer than half were real fraud. Whether that is acceptable depends on what a false alarm costs (a support call, an annoyed customer) compared with a missed fraud (a chargeback). The matrix makes that trade-off visible; a single accuracy figure hides it.
Metrics derived from the matrix
accuracy = (TP + TN) / total = (79 + 822) / 1000 = 0.901
precision = TP / (TP + FP) = 79 / 172 = 0.459
recall = TP / (TP + FN) = 79 / 85 = 0.929
specificity = TN / (TN + FP) = 822 / 915 = 0.898
F1 = 2PR / (P + R) = 0.615
- Precision: of everything flagged, how much was right. Matters when false alarms are expensive.
- Recall (sensitivity, true positive rate): of all real positives, how many were found. Matters when misses are expensive.
- Specificity: of all real negatives, how many were correctly left alone.
- F1: the harmonic mean of precision and recall, which is high only when both are high.
The accuracy trap
always predict "not fraud": TP 0, FP 0, FN 85, TN 915
accuracy 0.915 precision 0.0 recall 0.0 specificity 1.0 F1 0.0
A model that simply answers “genuine” for every transaction scored 91.5 percent accuracy, higher than our real model’s 90.1 percent, while catching zero frauds. With 8.5 percent positives, accuracy is dominated by the easy majority class. This is why accuracy is misleading for imbalanced problems such as fraud, disease screening, defect detection or spam. Look at the matrix, recall and precision instead, and always compare against this trivial baseline. Similar care applies to naive Bayes spam filters, where spam may be rare.
Changing the threshold
| Threshold | TP | FP | FN | TN | Precision | Recall | F1 |
|---|---|---|---|---|---|---|---|
| 0.3 | 85 | 473 | 0 | 442 | 0.152 | 1.000 | 0.264 |
| 0.4 | 84 | 241 | 1 | 674 | 0.258 | 0.988 | 0.410 |
| 0.5 | 79 | 93 | 6 | 822 | 0.459 | 0.929 | 0.615 |
| 0.6 | 60 | 29 | 25 | 886 | 0.674 | 0.706 | 0.690 |
| 0.7 | 41 | 6 | 44 | 909 | 0.872 | 0.482 | 0.621 |
The confusion matrix depends on the threshold, not just the model. At 0.3 every fraud was caught, at the cost of 473 false alarms; at 0.7 only 6 false alarms remained, but more than half the frauds slipped through. Precision and recall pull in opposite directions, and F1 peaked at 0.6 in this data. Plotting precision against recall across all thresholds gives the precision-recall curve, a fuller picture than any single matrix.
Choosing the threshold by cost
cost = FN * 500 + FP * 20 # a missed fraud costs 500, a false alarm 20
threshold 0.3: 9460
threshold 0.4: 5320
threshold 0.5: 4860 lowest
threshold 0.6: 13080
F1 treats precision and recall as equally important, which real businesses rarely do. Assigning a cost to each cell made the decision concrete: with a missed fraud 25 times as expensive as a false alarm, threshold 0.5 minimised cost, even though 0.6 had a better F1. Moving from 0.5 to 0.6 saved 64 false alarms but let 19 more frauds through, which cost far more. Agree these costs with the business before choosing a threshold, and revisit them when the costs change.
Tip: report the confusion matrix together with the threshold that produced it. A precision or recall figure without its threshold cannot be compared with anything.
Multiclass confusion matrices
# support tickets: rows = actual, columns = predicted
bug feature question
bug 109 13 12
feature 6 80 10
question 5 7 58
With more than two classes, the matrix grows to one row and column per class. Each row shows where the tickets of one true class ended up: 109 bugs were classified correctly, 13 were mistaken for feature requests and 12 for questions. Off-diagonal cells show which classes the model confuses, often the most useful insight for improving data or features. Per-class precision and recall come from treating each class in turn as positive and all others as negative.
bug precision 0.908 recall 0.813 F1 0.858 support 134
feature precision 0.800 recall 0.833 F1 0.816 support 96
question precision 0.725 recall 0.829 F1 0.773 support 70
Macro, weighted and micro averages
macro F1 0.816 weighted F1 0.825 micro F1 = accuracy 0.823
Macro averaging takes the plain mean of per-class scores, so small classes count as much as large ones; use it when every class matters equally. Weighted averaging weights by support (number of true examples per class), so it leans towards large classes. Micro averaging pools all predictions; for single-label multiclass problems it equals accuracy. Always state which one you report.
Normalised matrices
bug feature question
bug 0.81 0.10 0.09
feature 0.06 0.83 0.10
question 0.07 0.10 0.83
Dividing each row by its total turns counts into proportions, so the diagonal shows recall per class and classes of different sizes become comparable. Our model got about 81 to 83 percent of each class right, but the raw counts hid that the smallest class, questions, had the lowest precision. Show both: counts reveal volume, normalised rows reveal behaviour.
Practical tips
- Evaluate on held-out data, never on the training set; models such as unpruned decision trees score perfectly on their training data.
- Check the orientation: confirm which axis is actual and which is predicted.
- Start from the baseline: compare with always predicting the majority class.
- Pick the metric from the cost of errors, then choose the threshold on validation data.
- Watch for drift: recompute the matrix regularly in production; class balance and costs change.
- Reuse the idea in testing: a test suite is itself a classifier of builds, and false alarms are flaky tests; the same reasoning also applies to distance-based models such as k nearest neighbors.
Final thoughts
A confusion matrix turns “how good is the model?” into four honest counts. From them come precision, recall, specificity and F1, each answering a different business question. Our fraud example showed why that matters: a do-nothing model beat the real one on accuracy, F1 and cost disagreed on the best threshold, and per-class views revealed which ticket types were confused. Always show the matrix with its threshold, and choose metrics from what each kind of mistake actually costs.
Frequently asked questions
What is a confusion matrix?
A table that counts a classifier predictions against the true labels, showing true positives, false positives, false negatives and true negatives, so you can see exactly how it errs.
What is the difference between precision and recall?
Precision is the share of positive predictions that were correct. Recall is the share of actual positives that the model found. Raising one usually lowers the other.
Why is accuracy misleading for imbalanced data?
When one class is rare, predicting the common class every time gives high accuracy while finding nothing. In our example that scored 91.5 percent and caught zero frauds.
What is a good F1 score?
It depends on the problem and the baseline. Compare F1 with simple baselines and earlier models on the same data, and remember that F1 weights precision and recall equally.
How do I choose a classification threshold?
Compute the confusion matrix at several thresholds on validation data, assign a cost to false positives and false negatives, and pick the threshold with the lowest total cost.
Keep learning
Need help with testing, WordPress or SEO?
We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.