Find the highest classification threshold that still meets a target recall

Quick Overview

Write a function that uses scikit-learn's precision-recall curve to return the highest probability threshold whose recall still meets a target. It tests how the curve's arrays line up, how recall changes as the threshold rises, and what that choice does to false positives and precision.

Find the highest classification threshold that still meets a target recall

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Write `threshold_for_target_recall(y_true, y_proba, target_recall)` that returns, as a Python `float`, the highest threshold whose recall is still `>= target_recall`. Recall falls monotonically as the threshold rises, so this is the most selective threshold that meets the floor: it flags the fewest cases and has the fewest false positives among the thresholds that qualify. Requirements: - Use `sklearn.metrics.precision_recall_curve(y_true, y_proba)`. - `recall` has length `n_thresholds + 1`. Mask with `recall[:-1] >= target_recall` and return `float(thresholds[mask].max())`. - If no threshold achieves the target, return the smallest threshold. - A target of `1.0` is always met by the lowest threshold, and the returned threshold's recall must then be exactly `1.0`. - On the grader's fixed 12-element input with `target_recall=0.8` the answer is `0.48`, graded with `assert_allclose(rtol=1e-9)`. Example: ```python y_true = [1, 0, 1, 1, 0, 1, 0, 1] y_proba = [0.9, 0.8, 0.7, 0.6, 0.55, 0.5, 0.3, 0.2] threshold_for_target_recall(y_true, y_proba, 0.8) # 0.5 threshold_for_target_recall(y_true, y_proba, 0.5) # 0.6 threshold_for_target_recall(y_true, y_proba, 1.0) # 0.2 ``` There are five positives. At 0.5 the four positives scoring 0.9, 0.7, 0.6 and 0.5 are flagged (recall 0.8), and any higher threshold drops one of them. ```hint Line up the arrays Check the order `precision_recall_curve` returns its thresholds in, which entry of `recall` has no matching threshold, and which way recall moves as the threshold rises. ``` ### Constraints & Assumptions - `target_recall` is a float in (0, 1]. - `y_true` holds binary labels in {0, 1}, and `y_proba` holds the predicted probability of the positive class for the same samples. - A sample counts as predicted positive when its probability is at or above the threshold, as in scikit-learn. ### What a Strong Answer Covers - Aligns `recall[:-1]` with `thresholds` and explains why the last recall entry is dropped. - Uses the monotonicity of recall to argue that the largest qualifying threshold is the answer. - Returns a Python `float` and handles the target of 1.0 and the fallback case. - States what the chosen threshold optimizes (fewest flagged cases and false positives) and whether that also maximizes precision. ### Follow-up Questions - Does the highest qualifying threshold also give the best precision among the thresholds that meet the floor? - How would you extend this to pick the best F-beta point instead of a hard recall floor? - The threshold was chosen on one sample. How would you check that it still meets the recall floor on new data?

Overview: Write a function that uses scikit-learn's precision-recall curve to return the highest probability threshold whose recall still meets a target. It tests how the curve's arrays line up, how recall changes as the threshold rises, and what that choice does to false positives and precision.

General logo
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0

Write threshold_for_target_recall(y_true, y_proba, target_recall) that returns, as a Python float, the highest threshold whose recall is still >= target_recall. Recall falls monotonically as the threshold rises, so this is the most selective threshold that meets the floor: it flags the fewest cases and has the fewest false positives among the thresholds that qualify.

Requirements:

  • Use sklearn.metrics.precision_recall_curve(y_true, y_proba) .
  • recall has length n_thresholds + 1 . Mask with recall[:-1] >= target_recall and return float(thresholds[mask].max()) .
  • If no threshold achieves the target, return the smallest threshold.
  • A target of 1.0 is always met by the lowest threshold, and the returned threshold's recall must then be exactly 1.0 .
  • On the grader's fixed 12-element input with target_recall=0.8 the answer is 0.48 , graded with assert_allclose(rtol=1e-9) .

Example:

y_true = [1, 0, 1, 1, 0, 1, 0, 1]
y_proba = [0.9, 0.8, 0.7, 0.6, 0.55, 0.5, 0.3, 0.2]

threshold_for_target_recall(y_true, y_proba, 0.8)  # 0.5
threshold_for_target_recall(y_true, y_proba, 0.5)  # 0.6
threshold_for_target_recall(y_true, y_proba, 1.0)  # 0.2

There are five positives. At 0.5 the four positives scoring 0.9, 0.7, 0.6 and 0.5 are flagged (recall 0.8), and any higher threshold drops one of them.

Constraints & Assumptions

  • target_recall is a float in (0, 1].
  • y_true holds binary labels in {0, 1}, and y_proba holds the predicted probability of the positive class for the same samples.
  • A sample counts as predicted positive when its probability is at or above the threshold, as in scikit-learn.

What a Strong Answer Covers Guidance

  • Aligns recall[:-1] with thresholds and explains why the last recall entry is dropped.
  • Uses the monotonicity of recall to argue that the largest qualifying threshold is the answer.
  • Returns a Python float and handles the target of 1.0 and the fallback case.
  • States what the chosen threshold optimizes (fewest flagged cases and false positives) and whether that also maximizes precision.

Follow-up Questions Guidance

  • Does the highest qualifying threshold also give the best precision among the thresholds that meet the floor?
  • How would you extend this to pick the best F-beta point instead of a hard recall floor?
  • The threshold was chosen on one sample. How would you check that it still meets the recall floor on new data?