Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 6

Choosing the Right Metric: Accuracy, Precision, Recall, and F1-Score

Recommended resources

Welcome to your sixth lesson. In our previous session, we introduced regularization as a key technique to combat overfitting by penalizing large model weights. We concluded that tuning the regularization strength requires a reliable way to measure a model's performance. Today, we will build that crucial skill.

This lesson addresses the core task of evaluating classification models. While "accuracy" is an intuitive starting point, it can often be misleading, especially in the kinds of real-world scenarios you'll encounter in NLP. We will go beyond accuracy to understand a more powerful set of metrics: precision, recall, and the F1-score. Mastering these will allow you to select and justify the right metric for a given task, ensuring you can robustly assess and compare your models.

The Foundation: Understanding Prediction Outcomes

Before we can define any metric, we must first categorize the four possible outcomes of any single prediction in a binary classification task. Let's assume we are building a model to identify emails as either "spam" (the positive class) or "not spam" (the negative class). For any given email, the model's prediction can be:

  • True Positive (TP): The model predicts "spam," and the email is actually spam. A correct hit.
  • True Negative (TN): The model predicts "not spam," and the email is actually not spam. A correct rejection.
  • False Positive (FP): The model predicts "spam," but the email is actually not spam. A false alarm, also known as a Type I error.
  • False Negative (FN): The model predicts "not spam," but the email is actually spam. A miss, also known as a Type II error.

These four categories are the fundamental building blocks for all the metrics we will discuss. They are often organized into a 2x2 table called a confusion matrix.

The following video provides a clear, visual walkthrough of these four concepts using a "dog vs. not a dog" example. Watch the first part to solidify your understanding of TP, TN, FP, and FN.

Precision, Recall, F1 score, True Positive|Deep Learning Tutorial 19 (Tensorflow2.0, Keras & Python)

This video from the codebasics channel will help you build a concrete understanding of the four prediction outcomes.

Watch the segment from the beginning until the explanation of the four categories. Notice how the first word ("True" or "False") indicates if the prediction was correct, and the second word ("Positive" or "Negative") indicates what the model predicted.


Accuracy: The Deceptively Simple Metric

The most straightforward metric is accuracy. It answers the question: "Out of all predictions, what fraction was correct?"

While simple, accuracy has a major flaw: it can be deeply misleading on imbalanced datasets, where one class is far more frequent than the other.

Consider a model for detecting a rare medical condition that occurs in 1% of the population. A lazy model that always predicts "no condition" will achieve 99% accuracy. It's technically correct 99% of the time, but it's completely useless for its intended purpose because it fails to identify a single sick patient (it has zero True Positives). This is why we need more nuanced metrics.

The article "Beyond Accuracy" provides an excellent discussion on this very point.

A Deep Dive into Classification Metrics (Precision, Recall, F1 ...

This article from Manish Mazumder's Substack gives a great introduction to the limitations of accuracy.

Read the introduction and the section titled Metric 1: Accuracy. Pay close attention to the example of the imbalanced dataset.


Precision and Recall: A More Nuanced View

To overcome the limitations of accuracy, we turn to two other key metrics: precision and recall. These metrics focus specifically on the performance of the positive class.

Precision

Precision answers the question: "Of all the instances the model predicted as positive, how many were actually positive?"

  • High Precision means that when your model predicts the positive class, it is highly likely to be correct. This implies a low rate of False Positives.
  • When to prioritize Precision: When the cost of a False Positive is high. For example, in an email spam filter, incorrectly marking an important email as spam (an FP) is often much worse than letting one spam email through (an FN).

Recall

Recall (also known as Sensitivity or True Positive Rate) answers the question: "Of all the actual positive instances, how many did the model correctly identify?"

  • High Recall means that your model is good at finding all the positive instances. This implies a low rate of False Negatives.
  • When to prioritize Recall: When the cost of a False Negative is high. For example, in medical screening for a serious disease, failing to detect the disease in a sick patient (an FN) can have catastrophic consequences. It's better to have some false alarms (lower precision) than to miss a real case.

The following image provides a helpful visual diagram for these two metrics.

This diagram illustrates the relationship between relevant elements (actual positives) and retrieved elements (predicted positives). Precision is the ratio of True Positives to all retrieved elements, while Recall is the ratio of True Positives to all relevant elements.

The Precision-Recall Trade-off

Precision and recall are often in tension. A model can achieve 100% recall by simply predicting "positive" for every single data point, but its precision would be terrible (many False Positives). Conversely, a model can achieve very high precision by only making a positive prediction when it is extremely confident, but it will likely miss many true positive cases (low recall).

Choosing the right balance is not a modeling problem; it's a business or application problem. The following video offers a brilliant and memorable explanation of how to think about this trade-off.

MFML 044 - Precision vs recall

Cassie Kozyrkov provides an exceptionally intuitive way to think about when to prioritize precision versus recall.

Watch the segment from Precision vs. Recall. Her examples of a book recommender (prioritizing precision) and robbing a house (prioritizing recall) are perfect for building a lasting intuition.


F1-Score: The Harmonic Mean

So what do you do when you need a single number to compare models, but accuracy is misleading and you don't have a strong reason to prefer precision over recall? You use the F1-score.

The F1-score is the harmonic mean of precision and recall:

As a CS student with a strong math background, you'll appreciate why we use the harmonic mean and not a simple average. The harmonic mean heavily penalizes extreme values. If a model has a precision of 1.0 but a recall of 0.1, its F1-score will be low (around 0.18), reflecting its poor overall performance. A simple average would give a misleadingly high score of 0.55. The F1-score is therefore high only when both precision and recall are high.

The F1-score is often the default metric for classification tasks with imbalanced classes, as it provides a more balanced measure of performance than accuracy.


Selecting and Justifying Your Metric

The central skill is not just knowing the formulas, but being able to select and justify the appropriate metric for your specific problem. This decision should always be driven by the use case.

Let's consider a task related to your goal of working with the Dari language. Suppose you are building a system to automatically identify text documents written in Dari, distinguishing them from Farsi (Iranian Persian).

  • If you prioritize Precision: You are building a system to label documents for a high-purity Dari corpus. Any Farsi document mislabeled as Dari (a False Positive) would contaminate your dataset and is very costly. You would tune your model to be very conservative, accepting that you might miss some true Dari documents (lower recall).
  • If you prioritize Recall: You are building a system to flag all potentially Dari documents for a human linguist to review. Missing a document (a False Negative) is the worst outcome, as it would be lost from the study. You would tune your model to be very inclusive, accepting that the linguist will have to sift through some Farsi documents that were incorrectly flagged (lower precision).
  • If you use F1-Score: You are building a general-purpose language identification API. You need a balanced performance between not mislabeling Farsi as Dari and not missing actual Dari documents. The F1-score provides a single metric to optimize for this balanced goal.

This summary table neatly compares the metrics we've discussed.

This table compares Accuracy, Precision, Recall, and F1 Score, explaining what each measures, the scenarios where it works best, and where it fails.

To round out your understanding, let's explore a complete walkthrough that defines all these metrics and then calculates them for a few hypothetical models.

What Is Accuracy, Precision, Recall, and F1 Score? - labelf.ai

This article from labelf.ai provides clear definitions and a practical example evaluating four different models.

Start by reading the section on The fundamental concepts to reinforce your knowledge of TP/TN/FP/FN. Then read the sections defining Precision and Recall, followed by the definition of the F1 Score. Finally, review the section Evaluating the four models to see how these metrics are applied in practice, and read the Conclusion for a final summary.


Conclusion

In this lesson, we've moved beyond simple accuracy to a more sophisticated and practical set of evaluation metrics for classification. You now have the tools to measure model performance in a way that reflects real-world costs and priorities.

Key takeaways from today's lesson:

  • Accuracy is intuitive but can be highly misleading on imbalanced datasets.
  • Precision measures the correctness of positive predictions and is crucial when False Positives are costly.
  • Recall measures the completeness of positive predictions and is vital when False Negatives are costly.
  • The F1-Score provides a balanced measure of precision and recall via their harmonic mean, making it a robust default metric for many tasks, especially with class imbalance.
  • The choice of metric is context-dependent and should be justified based on the specific goals and error costs of your application.

Now that you know what to measure, our next lesson will address how to measure it reliably. We will introduce k-fold cross-validation, a powerful technique for getting a stable and robust estimate of your model's performance on unseen data, which is essential for tasks like tuning hyperparameters.