Precision-Recall Curve and Average Precision

Quick Overview

Implement a weighted precision-recall curve with tie handling and compute average precision as an uninterpolated step sum that matches scikit-learn. It tests cumulative counting over sorted scores, output array orientation, degenerate cases and the difference between step-wise AP and trapezoidal PR-AUC.

Precision-Recall Curve and Average Precision

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `precision_recall_curve(y_true, y_score, sample_weight=None)` returning `(precision, recall, thresholds)` as NumPy arrays. `thresholds` are the distinct scores in ascending order (tied scores share one threshold, none dropped), and entry `i` of `precision`/`recall` covers predicting positive when `score >= thresholds[i]`, with a final point `precision=1.0, recall=0.0` appended. Also implement `average_precision` as the step sum below (no interpolation, no trapezoid), returned as a `float`: $$\text{AP} = \sum_n (R_n - R_{n-1})\, P_n$$ Here $P_n$ and $R_n$ are the precision and recall at the n-th threshold, with thresholds taken from the highest score down and recall starting from $R_0 = 0$. Example: ```python y_true = [0, 0, 1, 1] y_score = [0.1, 0.4, 0.35, 0.8] ``` ```text precision -> [0.5, 0.6667, 0.5, 1.0, 1.0] recall -> [1.0, 1.0, 0.5, 0.5, 0.0] thresholds -> [0.1, 0.35, 0.4, 0.8] average_precision -> 0.8333 ``` ```hint Building the counts Sort once by descending score and keep running totals of weighted true and false positives. Think about which position inside a block of tied scores holds the counts for that threshold. ``` ```hint Lining up the step sum Only thresholds where recall changes contribute to AP. Check which precision value each recall gain should be paired with once the arrays are in ascending threshold order with the endpoint appended. ``` ### Constraints & Assumptions - Label `1` is positive, anything else negative. `sample_weight` weights TP/FP counts. - No positives: `recall` is all `1.0` (before the appended `0.0`) and `average_precision` returns `0.0`. Precision with a zero denominator is `0.0`. - Raise `ValueError` on empty input or when `y_true`, `y_score` or `sample_weight` lengths differ. - `average_precision` must match `sklearn.metrics.average_precision_score` (weighted and unweighted) to `1e-9`. ### What a Strong Answer Covers - Sorting by descending score and accumulating weighted true and false positive counts, with each block of tied scores collapsed to one threshold. - The output orientation: ascending thresholds, precision and recall aligned with them, and the appended endpoint with no extra threshold. - The degenerate cases: zero precision denominators, no positives, and empty or mismatched inputs. - Average precision as the uninterpolated step sum, and why it matches the sklearn value with weights and ties. - Time and space complexity. ### Follow-up Questions - Why is the step-wise sum preferred over trapezoidal PR-AUC? - How would you extend this to multi-class or multi-label problems? - When would you report PR-AUC instead of ROC-AUC?

Overview: Implement a weighted precision-recall curve with tie handling and compute average precision as an uninterpolated step sum that matches scikit-learn. It tests cumulative counting over sorted scores, output array orientation, degenerate cases and the difference between step-wise AP and trapezoidal PR-AUC.

General logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement precision_recall_curve(y_true, y_score, sample_weight=None) returning (precision, recall, thresholds) as NumPy arrays. thresholds are the distinct scores in ascending order (tied scores share one threshold, none dropped), and entry i of precision/recall covers predicting positive when score >= thresholds[i], with a final point precision=1.0, recall=0.0 appended.

Also implement average_precision as the step sum below (no interpolation, no trapezoid), returned as a float:

AP=∑n(Rn−Rn−1) Pn\text{AP} = \sum_n (R_n - R_{n-1})\, P_n

Here PnP_n and RnR_n are the precision and recall at the n-th threshold, with thresholds taken from the highest score down and recall starting from R0=0R_0 = 0.

Example:

y_true  = [0, 0, 1, 1]
y_score = [0.1, 0.4, 0.35, 0.8]
precision  -> [0.5, 0.6667, 0.5, 1.0, 1.0]
recall     -> [1.0, 1.0, 0.5, 0.5, 0.0]
thresholds -> [0.1, 0.35, 0.4, 0.8]
average_precision -> 0.8333

Constraints & Assumptions

  • Label 1 is positive, anything else negative. sample_weight weights TP/FP counts.
  • No positives: recall is all 1.0 (before the appended 0.0 ) and average_precision returns 0.0 . Precision with a zero denominator is 0.0 .
  • Raise ValueError on empty input or when y_true , y_score or sample_weight lengths differ.
  • average_precision must match sklearn.metrics.average_precision_score (weighted and unweighted) to 1e-9 .

What a Strong Answer Covers Guidance

  • Sorting by descending score and accumulating weighted true and false positive counts, with each block of tied scores collapsed to one threshold.
  • The output orientation: ascending thresholds, precision and recall aligned with them, and the appended endpoint with no extra threshold.
  • The degenerate cases: zero precision denominators, no positives, and empty or mismatched inputs.
  • Average precision as the uninterpolated step sum, and why it matches the sklearn value with weights and ties.
  • Time and space complexity.

Follow-up Questions Guidance

  • Why is the step-wise sum preferred over trapezoidal PR-AUC?
  • How would you extend this to multi-class or multi-label problems?
  • When would you report PR-AUC instead of ROC-AUC?