1004. Precision, Recall, and F1
Medium · Math
Given binary ground-truth labels and binary predictions, compute precision, recall, and F1 score.
precision = TP / (TP + FP) recall = TP / (TP + FN) F1 = 2 * P * R / (P + R)
Output as JSON, all values rounded to 4 decimals.
Input (stdin, JSON): { "y_true": [1, 0, 1, 1, 0], "y_pred": [1, 0, 0, 1, 1] }
Output (stdout): {"precision": 0.6667, "recall": 0.6667, "f1": 0.6667}
Edge case: when no predictions are positive (or no ground-truth positives), precision/recall = 0. When both are 0, F1 = 0.
Examples
Example 1
Input: {"y_true":[1,0,1,1,0],"y_pred":[1,0,0,1,1]}
Output: {"precision": 0.6667, "recall": 0.6667, "f1": 0.6667}Constraints
- Both lists same length, 1..10000.
- Values are 0 or 1.