Precision-Recall Curve Points
You are given detection results (whether each detection matched a ground truth object) and need to compute precision and recall at each detection to construct a PR curve.
Detections should be processed in order of decreasing confidence (highest first).
For each detection processed:
- Precision = True Positives / Total Detections So Far
- Recall = True Positives / Total Ground Truth Objects
Precision=TP+FPTP​Recall=TP+FNTP​=num_gtTP​
Where:
- TP = detections that matched a ground truth
- FP = detections that didn't match (false alarms)
- num_gt = total ground truth objects
Round precision and recall to 4 decimal places.
Example:
matches = [True, False, True] num_gt = 3
[(1.0, 0.3333), (0.5, 0.3333), (0.6667, 0.6667)]
Processing detections in order:
-
Detection 1: True (matched)
- TP = 1, Total detections = 1
- Precision = 1/1 = 1.0
- Recall = 1/3 = 0.3333
-
Detection 2: False (false positive)
- TP = 1, Total detections = 2
- Precision = 1/2 = 0.5
- Recall = 1/3 = 0.3333 (unchanged)
-
Detection 3: True (matched)
- TP = 2, Total detections = 3
- Precision = 2/3 = 0.6667
- Recall = 2/3 = 0.6667
Constraints:
- matches: list of booleans, True if detection matched ground truth, False otherwise
- Detections are already sorted by confidence (highest first)
- num_gt: total number of ground truth objects
- Return list of (precision, recall) tuples
You are building the per-detection points of a precision–recall (PR) curve, given an ordered list of detections and whether each one is a true positive or false positive.
1. Background Knowledge
In object detection, a model outputs many candidate detections, each with a confidence score. To evaluate the model, each detection is matched (or not) to a ground-truth object. A detection that correctly matches a ground truth is a true positive (TP); one that doesn’t match any ground truth (or matches something already claimed by another detection) is a false positive (FP). The number of ground-truth objects is fixed and known as num_gt.
The precision–recall curve summarizes performance as you move down the list of detections from high confidence to low confidence. At each prefix of the list (first 1 detection, first 2 detections, etc.), you can compute:
- Precision: fraction of processed detections that are correct.
- Recall: fraction of ground-truth objects that have been correctly detected.
Formally:
- Precision=TP+FPTP​
- Recall=\frac{TP}{TP + FN} = \frac{TP}{\text{num_gt}}
Plotting precision vs recall for all prefixes gives the PR curve. In this problem, you just need to output the precision and recall values for each prefix, rounded to 4 decimal places.
2. Algorithm / General Approach
This is a classic running-prefix statistics problem:
- You are given:
- A list of detections already sorted by decreasing confidence.
- For each detection, a boolean/flag: is this detection a TP or FP?
- A total num_gt (number of ground-truth objects).
- You process the detections in order, keeping cumulative counts of TPs and FPs.
- After processing each detection, compute:
- precision_i = \frac{\text{cumulative_TP}}{\text{detections_processed_so_far}}
- recall_i = \frac{\text{cumulative_TP}}{\text{num_gt}}
- Store or print these values for each step.
This pattern appears often in evaluation tasks: you walk over sorted predictions and maintain running statistics.
3. Step-by-Step Strategy
- Understand the inputs
- matches: an array where each element indicates whether the i-th detection is a TP (1 / true) or FP (0 / false).
- num_gt: total number of ground-truth objects (positive instances).
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.