Find the lowest classification threshold that meets a target precision
Quick Overview
Write a function that picks the lowest classification threshold whose precision meets a target, using scikit-learn's precision-recall curve. It tests how the curve's arrays line up, why precision does not move monotonically with the threshold, and how to handle a target that no threshold can reach.
Find the lowest classification threshold that meets a target precision
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `threshold_for_target_precision(y_true, y_proba, target_precision)`, returning as a Python `float` the lowest threshold whose precision is `>= target_precision`. The lowest feasible threshold maximizes recall. Use `sklearn.metrics.precision_recall_curve(y_true, y_proba)`, which returns `precision`, `recall` and `thresholds`.
The function must follow these rules:
- The trailing `precision=1.0` / `recall=0.0` point has no threshold and would satisfy any target, so it must not count as feasible.
- Precision is not monotonic in the threshold, so the answer cannot come from a binary search or from the first crossing.
- If no threshold meets the target, return `float('nan')`.
```hint Trace a small case
Write out precision at every threshold for a handful of scores where a negative sits between two positives. Watch what precision does as the threshold moves down.
```
```hint Check the array lengths
`precision_recall_curve` returns arrays of different lengths. Make sure each precision value you test belongs to the threshold you return.
```
### Constraints & Assumptions
- `y_true` is a 1-D array of 0/1 labels and `y_proba` is a same-length array of predicted probabilities for class 1.
- A threshold `t` means "predict positive when `y_proba >= t`", which is the convention `precision_recall_curve` uses.
- The grader tests `target_precision=0.75` and the unreachable `target_precision=1.01`. The second must return `nan` (checked with `np.isnan`); other results are compared with `assert_allclose(rtol=1e-9)`.
### What a Strong Answer Covers
- How `precision` lines up with `thresholds`, and why the trailing point must be excluded.
- Why the lowest feasible threshold gives the highest recall among the feasible thresholds.
- Why the non-monotonic precision curve rules out binary search and early stopping.
- The case where no threshold qualifies, and the return type.
### Follow-up Questions
- Why can't you binary-search for the threshold here, when you can for a recall target?
- How would you make the chosen threshold less likely to overfit one dataset?
Overview: Write a function that picks the lowest classification threshold whose precision meets a target, using scikit-learn's precision-recall curve. It tests how the curve's arrays line up, why precision does not move monotonically with the threshold, and how to handle a target that no threshold can reach.
Find the lowest classification threshold that meets a target precision
General
Nov 25, 2026
mediumData ScientistOtherMachine Learning
0
0
Implement threshold_for_target_precision(y_true, y_proba, target_precision), returning as a Python float the lowest threshold whose precision is >= target_precision. The lowest feasible threshold maximizes recall. Use sklearn.metrics.precision_recall_curve(y_true, y_proba), which returns precision, recall and thresholds.
The function must follow these rules:
The trailing
precision=1.0
/
recall=0.0
point has no threshold and would satisfy any target, so it must not count as feasible.
Precision is not monotonic in the threshold, so the answer cannot come from a binary search or from the first crossing.
If no threshold meets the target, return
float('nan')
.
Constraints & Assumptions
y_true
is a 1-D array of 0/1 labels and
y_proba
is a same-length array of predicted probabilities for class 1.
A threshold
t
means "predict positive when
y_proba >= t
", which is the convention
precision_recall_curve
uses.
The grader tests
target_precision=0.75
and the unreachable
target_precision=1.01
. The second must return
nan
(checked with
np.isnan
); other results are compared with
assert_allclose(rtol=1e-9)
.
What a Strong Answer Covers Guidance
How
precision
lines up with
thresholds
, and why the trailing point must be excluded.
Why the lowest feasible threshold gives the highest recall among the feasible thresholds.
Why the non-monotonic precision curve rules out binary search and early stopping.
The case where no threshold qualifies, and the return type.
Follow-up Questions Guidance
Why can't you binary-search for the threshold here, when you can for a recall target?
How would you make the chosen threshold less likely to overfit one dataset?