Binary Accuracy & Confusion Counts (TP, FP, TN, FN)

Basics
BasicsClassification MetricsConfusion MatrixGoogleMetaAmazon

Problem

Given binary lists y_true and y_pred (values in {0, 1}), implement confusion_counts(y_true, y_pred) returning a dictionary with keys 'tp', 'fp', 'tn', 'fn', and 'accuracy'.

Why It Matters: Decomposing predictions into TP, FP, TN, and FN exposes class-imbalance failure modes that raw accuracy hides.

Sample Inputs & Expected Outputs

Example 1 — Balanced 4 samples

>>> confusion_counts([1, 0, 1, 0], [1, 1, 0, 0])
{'tp': 1, 'fp': 1, 'tn': 1, 'fn': 1, 'accuracy': 0.5}

1 of each quadrant; accuracy = (1 + 1) / 4 = 0.5.

Example 2 — All-zero predictor on imbalanced data

>>> confusion_counts([0, 0, 0, 1], [0, 0, 0, 0])
{'tp': 0, 'fp': 0, 'tn': 3, 'fn': 1, 'accuracy': 0.75}

75% accuracy while catching 0 positives (TP=0, FN=1)!

Constraints & Edge Cases

  • Return a dict {'tp': int, 'fp': int, 'tn': int, 'fn': int, 'accuracy': float}.

Constraints & Evaluation Criteria

  • Return a dict `{'tp': int, 'fp': int, 'tn': int, 'fn': int, 'accuracy': float}`.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
[1,0,1,0] vs [1,1,0,0]
Expected Output / Criteria
tp=1,fp=1,tn=1,fn=1,acc=0.5