Choose a Threshold by Business Cost

easy
Unit 3 · Classification I0/3 to milestone

The default 0.5 threshold assumes a false positive costs the same as a false negative. In fraud detection, missing fraud might cost 500whileafalsealarmcosts500 while a false alarm costs 5.

Implement:

  • expected_cost(probs, labels, threshold, cost_fp, cost_fn): predict positive when prob >= threshold, and return FP * cost_fp + FN * cost_fn
  • best_threshold(probs, labels, cost_fp, cost_fn): try every distinct probability as a threshold, plus 1.0 + 1e-9 (predict nothing positive). Return (threshold, cost) with the lowest cost (ties: the smallest threshold).
  • bayes_threshold(cost_fp, cost_fn): the optimal threshold for calibrated probabilities, cost_fp / (cost_fp + cost_fn)
#classification#decision-theory#metrics2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Choose a Threshold by Business Cost · TechGradify