BasicsClassification MetricsF1GoogleMetaAmazon
Problem
Given integer counts tp, fp, and fn, implement precision_recall_f1(tp, fp, fn) returning (precision, recall, f1). Return 0.0 for any metric whose denominator is zero.
Why It Matters: Precision, recall, and F1-score govern imbalanced classification tasks like fraud detection, spam filtering, and medical screening.
Sample Inputs & Expected Outputs
Example 1 — Standard counts
>>> precision_recall_f1(tp=4, fp=1, fn=2)
(0.8, 0.6667, 0.7273)
Precision=4/5=0.8, Recall=4/6=0.6667, F1=20.80.6667/(0.8+0.6667)=0.7273.
Example 2 — Zero true positives
>>> precision_recall_f1(tp=0, fp=3, fn=5)
(0.0, 0.0, 0.0)
Safely returns 0.0 instead of dividing by zero when Precision + Recall = 0.
Example 3 — All zeros edge case
>>> precision_recall_f1(tp=0, fp=0, fn=0)
(0.0, 0.0, 0.0)
All zero denominators guarded safely.
Constraints & Edge Cases
- Return
(precision, recall, f1)as Python floats. - Safely return
0.0when denominators are zero.
Constraints & Evaluation Criteria
- Return `(precision, recall, f1)` as Python floats.
- Safely return `0.0` when denominators are zero.
Hints
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
tp=4, fp=1, fn=2
Expected Output / Criteria
(0.8, 0.6667, 0.7273)