Back to Practice
#0104

TPR and FPR From Counts

EasyML15 min10 XP

Problem

Given tp, fp, tn, fn, return {"tpr": tp/(tp+fn), "fpr": fp/(fp+tn)}. If a denominator is zero, return 0 for that rate.

Why This Matters

ROC curves and threshold tuning are built from TPR and FPR. The formulas need to become automatic.

Function Signature

def rates_from_counts(tp, fp, tn, fn):

Examples

Example 1
Inputtp=8, fp=2, tn=6, fn=4
Output{"tpr": 0.666..., "fpr": 0.25}

TPR uses actual positives; FPR uses actual negatives.

Constraints

  • Return the exact structure requested.
  • Handle the edge cases mentioned in the prompt.
  • Prefer clear logic over clever one-liners.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Inputtp=8, fp=2, tn=6, fn=4
Expected{"tpr": 0.666..., "fpr": 0.25}

TPR uses actual positives; FPR uses actual negatives.

Hidden Test Categories
zero positiveszero negativesperfect classifier