Workspace/Coding labs
Loading progress

Implement threshold evaluation with explicit edge cases

Foundation55 min

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

  1. Require equally sized nonempty lists of exact binary integer labels and finite real probability scores in [0,1]; reject booleans as scores.
  2. Require a finite numeric threshold in [0,1], excluding booleans. Predict positive when score >= threshold.
  3. Return tp, fp, fn, tn, precision, recall, f1, and log_loss. Define zero-denominator precision, recall, and F1 as 0.0.
  4. 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

One error of each kind.
EXAMPLE 2

Inputlabels=[0,0], scores=[0.1,0.2]

Outputprecision=recall=f1=0.0

The empty positive class uses the documented convention.
solution.pyPython 3.12