171. Build a precision recall curve and average precision with tied scores
Average precision summarises a ranking or a detector in one number, and it is the core of mAP in retrieval and object detection. Real scores often tie, for example rounded probabilities or a reranker that outputs a few levels. A correct curve treats tied scores as one threshold, because no threshold can separate them.
Write pr_curve(y_true, scores). y_true holds 0/1 labels, scores holds floats (higher means more likely positive). Use every distinct score as a threshold, from highest to lowest. At threshold t, everything with score >= t is predicted positive. Record precision = TP / (TP + FP) and recall = TP / (total positives) at each threshold.
Average precision is AP = sum over thresholds of (R_k - R_(k-1)) * P_k, with R_0 = 0.
Return a dict {"thresholds": [...], "precision": [...], "recall": [...], "ap": float}, with the three lists in threshold order (highest score first). If there are no positive labels, return all three lists empty and ap = 0.0.
def pr_curve(y_true, scores)Example 1
y_true = [1, 0, 1, 0], scores = [0.9, 0.5, 0.5, 0.1]. Thresholds [0.9, 0.5, 0.1], precision [1.0, 0.6667, 0.5], recall [0.5, 1.0, 1.0], ap = 0.5 * 1.0 + 0.5 * 0.6667 = 0.8333.
Example 2
Input: y_true = [0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0], scores = [0.0, 0.0, 0.0, 0.1, 0.0, 0.2, 0.4, 0.1, 0.4, 0.1, 0.0, 0.6, 0.2, 0.3, 0.2, 0.9, 0.3, 0.1, 0.4, 0.0, 0.5, 0.0, 0.3, 0.0, 0.0, 1.0, 0.3, 0.0, 0.3, 0.3]
Output: {'thresholds': [1.0, 0.9, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0], 'precision': [1.0, 1.0, 1.0, 1.0, 0.857143, 0.615385, 0.625, 0.55, 0.4], 'recall': [0.083333, 0.166667, 0.25, 0.333333, 0.5, 0.666667, 0.833333, 0.916667, 1.0], 'ap': 0.762088}
Constraints
- Python 3 with NumPy. Code runs in your browser.
- Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1
np.unique gives distinct scores in ascending order; reverse it.
Hint 2
For each threshold, count positives and negatives with score >= t.
Hint 3
A cumulative-sum version must jump to the end of each tie group.
Common mistakes
- Sorting and walking one item at a time, as the buggy version does, splits tied items into fake thresholds, so the answer depends on their arbitrary order.
Understand the idea
A precision recall curve asks: if I accept everything scored at least t, how clean is the accepted set (precision) and how much of the real positives did I catch (recall)? When two items share the same score, no threshold can split them, so they must enter the accepted set together as one step.
y_true = [0, 1, 1], scores = [0.9, 0.6, 0.6]. Threshold 0.9: TP 0, FP 1, so P = 0, R = 0. Threshold 0.6: both tied items join, TP 2, FP 1, so P = 0.667, R = 1. AP = (0 - 0) * 0 + (1 - 0) * 0.667 = 0.667.
walk the distinct scores from high to low, keep running TP and FP, record P = TP / (TP + FP) and R = TP / total positives, then AP = sum of (R_k - R_(k-1)) * P_k with R_0 = 0
With no positives, return empty lists and AP 0. Average precision is the building block of mAP in search ranking and object detection.