Compute precision and recall at a probability threshold

Quick Overview

Implement a NumPy function that thresholds predicted probabilities and returns precision and recall for the positive class. It tests confusion-matrix counting with boolean masks, the zero-division case when nothing is predicted positive, and how both metrics move as the threshold changes.

Compute precision and recall at a probability threshold

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `precision_recall_at_threshold(y_true, y_proba, threshold)`. `y_true` is a 1-D int ndarray of 0/1 labels and `y_proba` is a 1-D float ndarray of predicted probabilities for class 1, both of shape `(n,)`. Predict positive when `p >= threshold` and return `(precision, recall)` for label 1 as two Python floats. Use these definitions: - precision = `tp / (tp + fp)` - recall = `tp / (tp + fn)` - If `tp + fp == 0`, precision is `0.0` (the same as scikit-learn's `zero_division=0`). The function must never return `nan`, and it must not depend on a warning being ignored. For example: ```python import numpy as np y_true = np.array([0, 1, 1, 0, 1]) y_proba = np.array([0.2, 0.9, 0.4, 0.7, 0.6]) precision_recall_at_threshold(y_true, y_proba, 0.5) # predicted positive: idx 1, 3, 4 -> tp=2 (idx 1, 4), fp=1 (idx 3), fn=1 (idx 2) # (0.6666666666666666, 0.6666666666666666) precision_recall_at_threshold(y_true, y_proba, 0.99) # nothing predicted positive -> (0.0, 0.0) ``` ```hint Counting the cells Build one boolean mask for the prediction and combine it with masks on `y_true`. Each confusion-matrix cell is the sum of one combination, and then only the denominators need care. ``` ### Constraints & Assumptions - `y_true` and `y_proba` have the same length `n`. - `y_true` contains at least one positive. - `numpy` is available. - The grader uses a fixed 12-element input at thresholds 0.3, 0.5, 0.8 and 0.99 and compares results with `assert_allclose(rtol=1e-9)`. ### What a Strong Answer Covers - Applies the `>=` rule exactly, so a probability equal to the threshold counts as a positive prediction. - Counts tp, fp and fn with vectorized boolean masks in one pass. - Guards the precision denominator when nothing is predicted positive, and says what recall does when there are no actual positives. - Returns Python floats and states the time and space cost. - Explains how precision and recall move as the threshold rises. ### Follow-up Questions - How would you extend this to compute the full precision-recall curve without re-scanning the data at every threshold? - When should you also handle zero division for recall, and what should the function return then? - How do precision and recall each change as the threshold rises, and is either one monotone?

Overview: Implement a NumPy function that thresholds predicted probabilities and returns precision and recall for the positive class. It tests confusion-matrix counting with boolean masks, the zero-division case when nothing is predicted positive, and how both metrics move as the threshold changes.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement precision_recall_at_threshold(y_true, y_proba, threshold). y_true is a 1-D int ndarray of 0/1 labels and y_proba is a 1-D float ndarray of predicted probabilities for class 1, both of shape (n,). Predict positive when p >= threshold and return (precision, recall) for label 1 as two Python floats.

Use these definitions:

  • precision = tp / (tp + fp)
  • recall = tp / (tp + fn)
  • If tp + fp == 0 , precision is 0.0 (the same as scikit-learn's zero_division=0 ). The function must never return nan , and it must not depend on a warning being ignored.

For example:

import numpy as np

y_true = np.array([0, 1, 1, 0, 1])
y_proba = np.array([0.2, 0.9, 0.4, 0.7, 0.6])

precision_recall_at_threshold(y_true, y_proba, 0.5)
# predicted positive: idx 1, 3, 4 -> tp=2 (idx 1, 4), fp=1 (idx 3), fn=1 (idx 2)
# (0.6666666666666666, 0.6666666666666666)

precision_recall_at_threshold(y_true, y_proba, 0.99)
# nothing predicted positive -> (0.0, 0.0)

Constraints & Assumptions

  • y_true and y_proba have the same length n .
  • y_true contains at least one positive.
  • numpy is available.
  • The grader uses a fixed 12-element input at thresholds 0.3, 0.5, 0.8 and 0.99 and compares results with assert_allclose(rtol=1e-9) .

What a Strong Answer Covers Guidance

  • Applies the >= rule exactly, so a probability equal to the threshold counts as a positive prediction.
  • Counts tp, fp and fn with vectorized boolean masks in one pass.
  • Guards the precision denominator when nothing is predicted positive, and says what recall does when there are no actual positives.
  • Returns Python floats and states the time and space cost.
  • Explains how precision and recall move as the threshold rises.

Follow-up Questions Guidance

  • How would you extend this to compute the full precision-recall curve without re-scanning the data at every threshold?
  • When should you also handle zero division for recall, and what should the function return then?
  • How do precision and recall each change as the threshold rises, and is either one monotone?