confusion matrix precision recall

Evaluating Classifiers: Confusion Matrix, Precision, Recall, and F1

Your classifier says it’s 99% accurate. Should you believe it? Surprisingly, the answer is often no. Accuracy can hide serious problems, especially when your data is imbalanced. So how do you actually judge whether a classifier is any good? That’s where the confusion matrix, precision, recall, and F1 score come in, and this article walks you through each one clearly.

This post is the companion guide to Episode 51 of the Intelevo Machine Learning series on YouTube. The video covers every concept below with visuals and a live code demo. Either way, by the end of this article, you’ll know exactly how to evaluate a classifier honestly, instead of trusting a single misleading number.

A Quick Recap: From Classification to Evaluation

Before we tackle evaluation, let’s briefly revisit where we left off. In the previous episode, we explored multi-class classification strategies. One-vs-Rest trains one classifier per class, and each one runs its own solo campaign against everyone else. One-vs-One, meanwhile, trains one classifier per pair of classes, and the majority wins.

We also learned that scikit-learn already picks a sensible default strategy for most models. However, we quietly skipped over one important question. We built classifiers for two classes, then for many classes, but we never asked if those classifiers actually work well. That’s exactly what this episode addresses.

The Problem: Why “Accuracy” Can Lie to You

Let’s start with a scenario. Imagine a fraud detector that processes 1,000 transactions, and only 5 of them are actually fraud. Now, suppose this model simply predicts “not fraud” every single time. Technically, it’s still right 995 times out of 1,000.

That’s 99.5% accuracy. However, the model catches zero fraud cases, ever. Therefore, accuracy alone just hid a complete failure. Data scientists often call this the accuracy paradox, and it shows up constantly in real-world, imbalanced datasets.

As a result, we clearly need better tools than plain accuracy to judge a classifier’s real performance.

The Big Idea: Four Ways to Be Right or Wrong

Here’s the core insight. A classifier doesn’t just get things “right” or “wrong.” Instead, there are four distinct outcomes, and separating them tells a much richer story than accuracy ever could.

Throughout this article, we’ll use one simple analogy: an airport security guard, checking every bag. Every bag the guard checks falls into one of four outcomes. Once you see all four clearly, accuracy stops being the whole story.

In plain words, every prediction a classifier makes is either a hit, a miss, a false alarm, or a correct all-clear. Let’s meet these four outcomes properly.

Meet the Confusion Matrix

The confusion matrix organizes those four outcomes into a simple two-by-two grid. Here’s what each box means, using our security guard analogy:

  • True Positive (TP): The guard flags a real threat. That’s a correct catch.
  • False Negative (FN): The guard clears a real threat. That’s a dangerous miss.
  • False Positive (FP): The guard flags an innocent bag. That’s a false alarm.
  • True Negative (TN): The guard clears an innocent bag. That’s a correct, quiet pass.

We call this grid the confusion matrix, and every metric in this article comes directly from these four boxes. Once you understand this grid, precision, recall, and F1 become much easier to grasp.

Precision: Of What I Flagged, How Many Were Right?

Let’s start with precision. Precision asks a focused question: when the guard raises an alarm, how often is it actually a real threat?

High precision means very few false alarms. For instance, a guard who only stops someone when they’re truly certain has high precision, since every alarm they raise tends to be a real one. In short, think of precision as the trustworthiness of an alarm.

Recall: Of What Was Actually True, How Many Did I Catch?

Next comes recall, and it asks a different question entirely. Out of every real threat that walked through, how many did the guard actually catch?

High recall means very few missed threats. A guard who stops anyone even slightly suspicious has high recall, since almost no real threat slips past them. Consequently, think of recall as the thoroughness of the search.

Precision and Recall, in Two Simple Formulas

You don’t need to memorize these formulas, but they’re worth understanding. Here they are, in plain terms:

  • Precision = TP / (TP + FP) — correct alarms, out of all alarms raised.
  • Recall = TP / (TP + FN) — correct alarms, out of all real threats.

Let’s make this concrete with a worked example. Suppose there are 10 real threats. The guard flags 12 bags total, and correctly catches 9 of the 10 real threats. That means:

  • Precision = 9 / 12 = 75%
  • Recall = 9 / 10 = 90%

Notice how these two numbers measure completely different mistakes. Precision cares about false alarms. Recall, on the other hand, cares about missed threats.

Why You Can’t Max Out Both

Here’s an important tension you’ll encounter constantly. A stricter guard raises precision, but risks missing real threats. Meanwhile, a looser guard raises recall, but drowns you in false alarms. Push one metric up, and the other tends to slide down.

Consider the strict guard first. This guard is rarely wrong when it does flag something, since it only stops someone when truly certain. However, real threats can slip through unnoticed.

Now consider the lenient guard instead. This guard rarely misses a real threat, since it stops anyone even slightly suspicious. That said, it floods you with false alarms.

Ultimately, neither approach is universally “better.” The right balance depends entirely on your specific problem.

F1 Score: One Number for Both

So, how do you balance precision and recall? That’s exactly what the F1 score does. It blends both metrics into a single number, so you don’t have to juggle two separate scores at once.

Here’s the formula:

F1 = 2 × (Precision × Recall) / (Precision + Recall)

Statisticians call this the harmonic mean of precision and recall. In plain words, F1 stays low unless BOTH precision and recall are reasonably high. Therefore, it punishes any classifier that’s great at one metric and terrible at the other, which makes it a genuinely balanced measure.

Choosing the Right Metric

By now, you might be wondering which metric to actually use. Fortunately, a simple guide can help:

  • If missing a case is dangerous, such as disease detection or fraud, prioritize recall.
  • If false alarms are costly, such as a spam folder or legal action, prioritize precision.
  • If both matter and your classes are imbalanced, use the F1 score.
  • If your classes are balanced and mistakes cost about the same either way, plain accuracy works fine.

As a rule of thumb, let the real-world cost of a mistake choose your metric. Don’t let the metric choose your problem’s priorities for you.

Real-World Examples of Each Metric

Let’s ground these ideas with a few concrete scenarios, since abstract formulas only click once you see them applied.

Medical diagnosis. Imagine a model that screens patients for a serious illness. Here, missing a real case is far more dangerous than a false alarm, since a missed diagnosis can cost a life. Therefore, doctors typically prioritize recall, even if that means a few extra follow-up tests for healthy patients.

Spam filtering. Now, consider an email spam filter instead. Here, a false alarm means an important email lands in the spam folder, which frustrates users badly. Consequently, precision usually matters more than recall in this scenario, since users tolerate an occasional spam message far better than a lost job offer.

Fraud detection. Fraud detection often needs both precision and recall simultaneously. Missing fraud is costly, but flooding customers with false fraud alerts damages trust just as quickly. As a result, teams often optimize for F1 score here, since it balances both concerns at once.

Manufacturing quality control. Finally, picture a balanced dataset where roughly half of all products pass and half fail inspection, with mistakes costing about the same either way. In this case, plain accuracy works perfectly well, and you don’t need the added complexity of precision, recall, or F1.

Notice the pattern here. Every single decision traces back to one question: what does a mistake actually cost in this specific context? Once you answer that question, choosing the right metric becomes straightforward.

Seeing It in Code: A Python Walkthrough

Now, let’s put all of this into practice. We’ll build a small, illustrative example that mirrors our worked example above.

from sklearn.metrics import (
    confusion_matrix, precision_score,
    recall_score, f1_score
)

# 1 = threat, 0 = safe  (20 bags: 10 real threats, 10 safe)
y_true = [1,1,1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0,0,0]
y_pred = [1,1,1,1,1,1,1,1,1,0, 1,1,1,0,0,0,0,0,0,0]

print(confusion_matrix(y_true, y_pred))
print('Precision:', precision_score(y_true, y_pred))
print('Recall:', recall_score(y_true, y_pred))
print('F1 Score:', f1_score(y_true, y_pred))

Let’s break this down, step by step. First, we import confusion_matrix, precision_score, recall_score, and f1_score from sklearn.metrics. Next, we set up a small dataset: 20 bags total, where 1 means threat and 0 means safe, split evenly into 10 real threats and 10 safe bags.

The y_true list holds the actual labels. Meanwhile, y_pred holds what our classifier predicted. Notice that y_pred catches nine of the ten real threats, misses one, and raises three false alarms on safe bags. These are the exact same numbers from our earlier worked example, so the results should match.

Finally, we print the confusion matrix, followed by precision, recall, and F1 score, each computed directly by scikit-learn from these two lists.

Reading the Output

Running this code produces results like the following:

[[7 3]
 [1 9]]
Precision: 0.75
Recall: 0.9
F1 Score: 0.8182

Let’s interpret this matrix. It reads as 7 true negatives, 3 false positives, 1 false negative, and 9 true positives. As expected, precision lands at 0.75, or 75%, and recall lands at 0.9, or 90%, exactly matching our worked example. The F1 score sits neatly between them at roughly 0.82.

Want to explore further? Try printing classification_report(y_true, y_pred) for a complete precision, recall, and F1 summary in one step. Alternatively, adjust y_pred to be stricter or looser by hand, and watch precision and recall move in opposite directions. Finally, try ConfusionMatrixDisplay to visualize the matrix instead of reading raw numbers.

Common Pitfalls to Avoid

Before wrapping up, let’s cover a few mistakes that trip people up during model evaluation.

Trusting accuracy blindly. On imbalanced data, a lazy model can score high accuracy while catching nothing important, just like our fraud detector example. Always look beyond accuracy.

Chasing one metric only. Optimizing purely for recall, or purely for precision, quietly wrecks the other metric. Instead, always check both together.

Ignoring class imbalance. Precision, recall, and F1 all shift meaning once one class vastly outnumbers the other. So, always understand your class distribution first.

Using the wrong metric for the problem. A spam filter and a cancer screener need different yardsticks. After all, the real-world cost of their mistakes differs enormously.

Five Things to Remember

Let’s lock in the key ideas before you go:

  1. Accuracy alone can hide a completely useless model.
  2. The confusion matrix splits every prediction into four outcomes: TP, FP, FN, and TN.
  3. Precision equals correct alarms divided by all alarms raised. Recall equals correct alarms divided by all real cases.
  4. F1 balances precision and recall into a single number.
  5. Let the real-world cost of a mistake choose your metric.

Together, these five ideas cover almost everything you need to evaluate classifiers confidently in your own projects.

Watch the Full Video

Reading through these concepts is a great start. However, seeing them explained visually, alongside a live walkthrough of the code, often makes everything click faster. Episode 51 of the Intelevo Machine Learning series covers this entire topic step by step, including the same code example from this article.

You’ll find the video on the Intelevo YouTube channel. If this article helped clarify the confusion matrix, precision, recall, and F1 score for you, please consider watching the video too. Don’t forget to like, subscribe, and leave a comment with your thoughts or questions. Your feedback genuinely helps this series reach more learners.

What’s Next

In Episode 52, we shift gears entirely. Instead of learning another concept, we’ll apply everything we’ve covered so far to a real business problem: a mini project on customer churn prediction. We’ll combine classification, multi-class strategies, and the evaluation techniques from this article into one complete, practical workflow.

Until then, keep practicing with precision, recall, and F1 on your own datasets. The best way to internalize these ideas is to compute them yourself, compare the results, and see exactly how each metric responds as your data changes.

Leave a Comment

Your email address will not be published. Required fields are marked *