Confusion Matrix Builder
You are given lists of predictions and ground truth labels, and need to build a confusion matrix.
A confusion matrix C is a square matrix where:
- C[i][j] = count of samples with true class i that were predicted as class j
- Diagonal entries C[i][i] are correct predictions
- Off-diagonal entries are misclassifications
The matrix provides detailed insight into which classes are being confused with each other.
For a 3-class problem, the structure is: C=βTP0βE1β0βE2β0ββE0β1βTP1βE2β1ββE0β2βE1β2βTP2βββ
Where TPiβ is true positives for class i, and Eiβjβ is errors where class i was predicted as class j.
Example:
predictions = [0, 1, 0, 1] ground_truth = [0, 0, 0, 1] num_classes = 2
[[2, 1], [0, 1]]
For each sample, increment C[true][predicted]:
- Sample 0: true=0, pred=0 β C[0][0]++ β [[1,0],[0,0]]
- Sample 1: true=0, pred=1 β C[0][1]++ β [[1,1],[0,0]]
- Sample 2: true=0, pred=0 β C[0][0]++ β [[2,1],[0,0]]
- Sample 3: true=1, pred=1 β C[1][1]++ β [[2,1],[0,1]]
Reading the matrix:
- C[0][0]=2: 2 samples of class 0 correctly predicted
- C[0][1]=1: 1 sample of class 0 wrongly predicted as class 1
- C[1][1]=1: 1 sample of class 1 correctly predicted
Constraints:
- predictions and ground_truth are lists of class indices of the same length
- num_classes is the total number of classes
- All class indices are in range [0, num_classes-1]
- Return the confusion matrix as a 2D list
You are building a counting data structure: given true labels and predicted labels for a multi-class classifier, you must tally how often each trueβpredicted pair occurs and store it in a square matrix.
1. Background Knowledge
In multi-class classification, each sample belongs to exactly one of K classes (e.g., 0, 1, 2 for a 3-class problem). A trained model outputs a predicted class for each input. To evaluate how well the model performs per class, you use a confusion matrix instead of just overall accuracy.
A confusion matrix CβRKΓK is defined so that:
- Rows correspond to true (ground-truth) classes.
- Columns correspond to predicted classes.
- Entry C[i][j] is the number of samples whose true class is i and predicted class is j.
Thus:
- Diagonal entries C[i][i] = correct predictions (true positives for class i).
- Off-diagonal entries show which classes are being confused (e.g., many counts in C means class 0 is often predicted as class 2).
2. Algorithm / Approach
General pattern:
- Determine the number of classes K.
- Initialize a KΓK matrix of zeros.
- Iterate once over all samples:
- Let t = true_label[i], p = pred_label[i].
- Increment C[t][p] by 1.
- Return the matrix.
This is essentially frequency counting over pairs (true, pred) using array indices.
3. Step-by-Step Strategy
- Identify class index range
- Often labels are integers from 0 to K-1.
- You may be given num_classes, or you may compute:
K = max(max(true_labels), max(pred_labels)) + 1
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.