Implement threshold evaluation with explicit edge cases
Implement evaluate_binary(labels, scores, threshold) to return confusion counts, precision, recall, F1, and mean log loss. Validate inputs so the metric cannot silently truncate or accept invalid probabilities.
Your task
- Require equally sized nonempty lists of exact binary integer labels and finite real probability scores in [0,1]; reject booleans as scores.
- Require a finite numeric threshold in [0,1], excluding booleans. Predict positive when score >= threshold.
- Return tp, fp, fn, tn, precision, recall, f1, and log_loss. Define zero-denominator precision, recall, and F1 as 0.0.
- For log loss only, clip probabilities to [1e-12, 1-1e-12]. Raise ValueError for invalid input.
Examples
EXAMPLE 1
Inputlabels=[1,0,1,0], scores=[0.9,0.6,0.4,0.1], threshold=0.5
Outputtp=1, fp=1, fn=1, tn=1, precision=recall=f1=0.5
EXAMPLE 2
Inputlabels=[0,0], scores=[0.1,0.2]
Outputprecision=recall=f1=0.0
Implement the function, then run the tests.
Ctrl / ⌘ + Enter to test · Shift + Tab leaves editor