Back to Practice
#0010
Build a Confusion Matrix
EasyML15 min10 XP
Problem
Given true binary labels and predicted binary labels, return a dictionary with TP, TN, FP, and FN counts.
Why This Matters
Accuracy can hide failure. Confusion matrix counts show exactly which kind of mistake the model is making.
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, "TN": 1, "FP": 1, "FN": 1}
Each of the four possible classification outcomes happens once.
Constraints
- Labels are 0 or 1.
- y_true and y_pred have the same length.
- Return integer counts.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 4 hidden categories
All outcome types
Input[1, 0, 1, 0], [1, 1, 0, 0]
Expected{"TP": 1, "TN": 1, "FP": 1, "FN": 1}
This test ensures every branch is handled.
Hidden Test Categories
All predictions correctAll predictions positiveNo positive true labelsLonger lists