#364PR-AUC (Average Precision)Medium

PR-AUC (Average Precision)

Background

On imbalanced data (fraud, click-through, retrieval), ROC-AUC is misleading because the false-positive rate is meaningless when negatives dominate. Average Precision (AP) = area under the PR curve is the canonical replacement: it averages precision over every recall threshold, focusing the metric on the minority class.

Problem statement

Implement pr_auc(y_true, scores) returning AP:

AP  =  i(RiRi1)Pi\text{AP} \;=\; \sum_{i}\, (R_i - R_{i-1})\, P_i

where the indices ii walk decreasing score order. PiP_i, RiR_i are precision and recall after seeing the top-ii scored items.

Input

  • y_true - array-like of {0,1}\{0, 1\} labels, length nn.
  • scores - array-like of floats, predicted scores. Higher = more likely positive.

Output

  • float in [0,1][0, 1]. Returns 00 if no positives in y_true.

Examples

Example 1 - perfect ranking

Input:  y_true=[1,1,0,0], scores=[0.9, 0.8, 0.7, 0.6]
Output: 1.0

Example 2 - inverted ranking

Input:  y_true=[0,0,1,1], scores=[0.9, 0.8, 0.7, 0.6]
Output: 0.4167...

Explanation: walking top-down, P=[0,0,1/3,1/2], R=[0,0,1/2,1]; AP = (1/2)(1/3) + (1/2)(1/2) = 0.4166. Add the boundary R0=0R_0=0 correctly.

Constraints

  • Sort by score descending. Ties broken arbitrarily but deterministically.
  • Empty input or zero positives -> return 0.00.0.
  • Return a Python float.

Notes

  • Why AP, not ROC-AUC. ROC plots TPR vs FPR; at 0.1% prevalence, FPR is dominated by trivial negatives and the curve looks great regardless. AP only cares about precision at each recall - the minority class drives the score.
  • Tie handling. Production implementations interpolate ties (sklearn's average_precision_score uses a step-function variant). The simple per-item form above is the most common interview answer.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • reference inverted ranking
  • example interleaved labels
  • sample scattered positives