📘
Confusion Matrix
EasyModel Evaluation
Build a confusion matrix for binary classification results.
Given lists of true labels and predicted labels (each 0 or 1), compute the 2x2 confusion matrix:
[TNFNFPTP]
where:
- TP (True Positive): predicted 1, actual 1
- TN (True Negative): predicted 0, actual 0
- FP (False Positive): predicted 1, actual 0
- FN (False Negative): predicted 0, actual 1
Return the matrix as a 2D list [[TN, FP], [FN, TP]].
Example:
Input:
y_true = [1, 0, 1, 1, 0, 0] y_pred = [1, 0, 0, 1, 0, 1]
Output:
[[2, 1], [1, 2]]
Reasoning:
- We iterate over the
y_trueandy_predlists simultaneously, comparing each pair of true and predicted labels. - For each pair, we check the conditions for TP, TN, FP, and FN and increment the corresponding counter:
- TP if ytrue=1 and ypred=1,
- TN if ytrue=0 and ypred=0,
- FP if ytrue=0 and ypred=1,
- FN if ytrue=1 and ypred=0.
- After iterating over all pairs, we count:
- TN: 2 (for the pairs (0,0) at indices 1 and 4),
- FP: 1 (for the pair (0,1) at index 5),
- FN: 1 (for the pair (1,0) at index 2),
- TP: 2 (for the pairs (1,1) at indices 0 and 3).
- The final output is the 2x2 confusion matrix: [[TN,FP],[FN,TP]]=[[2,1],[1,2]].
Constraints:
- y_true and y_pred are lists of 0s and 1s of equal length
- Return a 2D list [[TN, FP], [FN, TP]]
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.