← Lesson
Rare Classes and Thresholds
Joeven
Run
Reset
Python loads on first run
# scores for 10 tickets, three are truly positive scores = [0.05, 0.10, 0.12, 0.18, 0.25, 0.40, 0.55, 0.70, 0.82, 0.91] y = [0, 0, 0, 0, 0, 0, 1, 0, 1, 1] def at_cutoff(c): pred = [1 if s >= c else 0 for s in scores] tp = sum(t == 1 and p == 1 for t, p in zip(y, pred)) fp = sum(t == 0 and p == 1 for t, p in zip(y, pred)) fn = sum(t == 1 and p == 0 for t, p in zip(y, pred)) prec = tp / (tp + fp) if tp + fp else 0.0 rec = tp / (tp + fn) if tp + fn else 0.0 return prec, rec, pred for c in (0.5, 0.3, 0.8): prec, rec, pred = at_cutoff(c) print("cutoff", c, "pred", pred, "precision", round(prec, 2), "recall", round(rec, 2))
Run to execute this in your browser. Nothing is sent to a server.