Edit the samples (true class and confidence) or choose a predefined scenario to visualize the confusion matrix, the P-S curve, and the mAP value in real time.
| Threshold | Prec. | P.mono | Sens. |
|---|
In this activity, you will evaluate a binary classifier (e.g., deforestation detection in satellite images, see dgi.inpe.br) using the Precision-Recall curve and the mAP (Mean Average Precision) metric. mAP is standard in competitions such as COCO (Common Objects in Context) and PASCAL VOC (Visual Object Classes) and in YOLO (You Only Look Once) models.
In EP01_02, you saw that the choice of threshold significantly alters Precision and Recall. The mAP (Mean Average Precision) addresses this: it evaluates the model at multiple thresholds (each threshold should generate a different confusion matrix) and summarizes performance by the area under the Precision-Recall (P-R) curve.
While the F1-Score examines a single equilibrium point, mAP considers the entire curve. The closer to 1.0, the better the detector across all thresholds and classes (e.g., coins of 25, 50, and 1 real).
| Metric | What it summarizes | Limitation |
|---|---|---|
| F1-Score | P × R balance at a single threshold | Depends on the chosen threshold |
| AP | Area under the P-R curve for one class | Valid only for a single class |
| mAP | Average of APs across all classes | More complex to implement |
References: Roboflow — mAP · Explanatory video
Fixed thresholds (always use this list):
limiares = [0.00, 0.09, 0.21, 0.31, 0.39, 0.52, 0.60, 0.71, 0.81, 0.89, 1.00]For each threshold (t), classify the samples: predito = 1 if confiança ≥ t, else 0.
Compute TP, FP, FN, TN and obtain Precision((t)) and Recall((t)).
Build the P-R curve: pairs (Recall((t)), Precision((t))), ordered by increasing Recall.
Monotonize Precision: \[P_{\text{mono}}[i] = \max_{j \ge i} P[j]\]
Compute the AP (area under the monotonic curve) using the trapezoidal rule (a more accurate approximation than the simple Riemann sum): \[AP = \sum_{i=1}^{m-1} \frac{P_{\text{mono}}[i-1] + P_{\text{mono}}[i]}{2} \cdot (S[i] - S[i-1])\]
mAP = average of the APs across all classes. In this assignment, there is only 1 class, so mAP = AP.
📐 Summary of the difference:
The Riemann sum approximates the area using rectangles, which may underestimate or overestimate. The trapezoidal rule uses trapezoids, reducing error by considering the average of the values at the interval endpoints, and is generally more accurate for piecewise smooth functions, such as the Precision-Recall curve.
Read an integer n (number of samples). Then read n lines, each containing: true (0 or 1) and confidence (float 0.0–1.0).
Calculate and print, for the threshold 0.85 (index 9 in the list):
Then, for all thresholds, print:
,| Input | Expected Output |
|---|---|
| 7 0 0.94 1 0.80 1 0.69 0 0.67 1 0.30 1 0.15 1 0.15 |
# METRICS FOR THRESHOLD 0.85 # Confusion Matrix: TP = 0, FN = 5 FP = 1, TN = 1 Evaluation Metrics: Accuracy: 0.14 Precision: 0.00 Recall: 0.00 F1-Score: 0.00 # METRICS FOR ALL THRESHOLDS # Precisions: 0.00, 0.00, 0.00, 0.50, 0.50, 0.50, 0.50, 0.50, 0.60, 0.71, 0.71 Monotonic Precisions: 0.71, 0.71, 0.71, 0.71, 0.71, 0.71, 0.71, 0.71, 0.71, 0.71, 0.71 Recalls: 0.00, 0.00, 0.00, 0.20, 0.40, 0.40, 0.40, 0.40, 0.60, 1.00, 1.00 mAP: 0.71 |
def calcular_AP(verdades, confiancas, limiares):
m = len(limiares)
precisoes = [0.0] * m
sensibilidades = [0.0] * m
for i in range(m):
p, s = calcular_metricas(verdades, confiancas, limiares[i])
precisoes[m-1-i] = p
sensibilidades[m-1-i] = s
prec_mono = precisoes.copy()
for i in range(m-2, -1, -1):
if prec_mono[i] < prec_mono[i+1]:
prec_mono[i] = prec_mono[i+1]
AP = 0.0
for i in range(1, m):
# Trapezoid rule: average of heights times the base
area_trapezio = (prec_mono[i-1] + prec_mono[i]) / 2.0
AP += area_trapezio * (sensibilidades[i] - sensibilidades[i-1])
return precisoes, prec_mono, sensibilidades, APEdit the samples (true class and confidence) or choose a predefined scenario to visualize the confusion matrix, the P-S curve, and the mAP value in real time.
| Threshold | Prec. | P.mono | Sens. |
|---|
# your solutionTestSuite("EP01_03.py").run()✔️ EP01_03.cases already exists in casos/ 📋 5 case(s) loaded from casos/EP01_03.cases 💥 File EP01_03.py not found.