Back to Practice
#0203

Binary Confusion Matrix

MediumML25 min20 XP

Problem

Return a dictionary with tp, fp, tn, and fn for binary labels where 1 is positive.

Why This Matters

Precision, recall, specificity, F1, and many interview questions all start from these four counts.

Function Signature

def confusion_counts(y_true, y_pred):

Examples

Example 1
Inputy_true = [1,0,1,0], y_pred = [1,1,0,0]
Output{"tp": 1, "fp": 1, "tn": 1, "fn": 1}

There is one case in each confusion-matrix cell.

Constraints

  • Return the exact requested result.
  • Handle common edge cases.
  • Keep the solution readable before optimizing.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Inputy_true = [1,0,1,0], y_pred = [1,1,0,0]
Expected{"tp": 1, "fp": 1, "tn": 1, "fn": 1}

There is one case in each confusion-matrix cell.

Hidden Test Categories
empty or minimal inputrepeated valueslarger realistic input