precision
recall
F-score
python
data processing

Calculating Precision, Recall and F-score in one pass - python

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

Introduction

In binary classification tasks, evaluating the performance of a model is crucial to understand its strengths and limitations. Among the various metrics used, precision, recall, and F-score are particularly important. These metrics help to assess the balance between the correctness and completeness of the model's predictions. This article explains how to calculate these metrics in a single pass through the data, using Python.

Technical Explanations

Precision, Recall, and F-Score Definitions

  • Precision is the ratio of correctly predicted positive observations to the total predicted positives: Precision=True PositivesTrue Positives+False PositivesPrecision = \frac{True\ Positives}{True\ Positives + False\ Positives}
  • Recall (Sensitivity) is the ratio of correctly predicted positive observations to all actual positives: Recall=True PositivesTrue Positives+False NegativesRecall = \frac{True\ Positives}{True\ Positives + False\ Negatives}
  • F-Score is the weighted harmonic mean of precision and recall, a common choice being the F1-score with equal weight: F1=2⋅Precision⋅RecallPrecision+RecallF1 = 2 \cdot \frac{Precision \cdot Recall}{Precision + Recall}

One-Pass Calculation

Typically, to compute these metrics, multiple passes over the dataset are performed to first gather true positive, false positive, and false negative counts, followed by calculating metrics. However, it's possible to achieve this in a single pass.

Implementing in Python

Single-Pass Algorithm

To compute precision, recall, and F-score in one pass, we incrementally update counts for true positives, false positives, and false negatives as we iterate through each instance in the dataset.

  1. Initialize Counters:
    • `TP` (True Positives)
    • `FP` (False Positives)
    • `FN` (False Negatives)
  2. Single Iteration:
    • For each instance, update `TP`, `FP`, and `FN` based on comparison between predicted and actual labels.
  3. Calculate Metrics:
    • Compute precision, recall, and F-score at the end of the iteration.

Python Code Example

Here is the implementation in Python:

  • If `TP + FP` is zero, precision is undefined — typically set to zero to indicate no positive predictions.
  • If `TP + FN` is zero, recall is zero since there are no actual positive cases.
  • If precision and recall are both zero, F1-score is also undefined and can be set to zero.
  • Imbalanced Datasets: Precision-recall is more informative than accuracy, especially for imbalanced classes.
  • Threshold Tuning: Precision and recall can help decide on a suitable threshold for predictions.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms