Confusion Matrix
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:
[TNFN​FPTP​]
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:
y_true = [1, 0, 1, 1, 0, 0] y_pred = [1, 0, 0, 1, 0, 1]
[[2, 1], [1, 2]]
- 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]]
Background Knowledge
The confusion matrix is a fundamental concept in model evaluation, particularly for binary classification problems. It provides a summary of predictions against actual outcomes, helping to evaluate the performance of a machine learning model. The matrix itself is a 2x2 table that categorizes predictions into four types: True Positive (TP), True Negative (TN), False Positive (FP), and False Negative (FN). Understanding these categories is crucial for assessing the accuracy, precision, recall, and F1-score of a model.
In the context of binary classification, each instance is labeled as either 0 or 1, representing two distinct classes. The model predicts a label for each instance, and the confusion matrix compares these predictions against the actual labels. The diagonal elements of the matrix, TP and TN, represent correct predictions, while the off-diagonal elements, FP and FN, represent incorrect predictions. This matrix is essential for identifying the strengths and weaknesses of a model, such as its tendency to overpredict or underpredict certain classes.
The confusion matrix is closely related to other evaluation metrics, such as accuracy, precision, recall, and the F1-score. These metrics can be calculated directly from the confusion matrix, providing a more detailed understanding of the model's performance. For example, accuracy is the ratio of correct predictions (TP + TN) to the total number of instances, while precision is the ratio of TP to the sum of TP and FP.
Algorithm/Approach
The general approach to building a confusion matrix involves iterating through the lists of true labels and predicted labels, comparing each pair of labels, and incrementing the corresponding cell in the matrix. This process can be implemented using a simple loop or by utilizing vectorized operations in languages like Python.
Step-by-Step Strategy
To implement the solution:
- Initialize a 2x2 matrix with zeros, representing the confusion matrix.
- Iterate through the lists of true labels and predicted labels simultaneously.
- For each pair of labels, compare the true label with the predicted label and update the corresponding cell in the matrix.
- After iterating through all pairs of labels, return the completed confusion matrix.
Common Pitfalls
When implementing the solution, watch out for:
- Incorrectly initializing the matrix or updating the wrong cells.
- Failing to handle edge cases, such as empty lists of labels.
- Not validating the input lists to ensure they have the same length and contain only binary labels (0 or 1).
Time & Space Complexity
The expected time complexity is O(n), where n is the length of the input lists, since we need to iterate through each pair of labels once. The space complexity is O(1), as we only need to store the 2x2 confusion matrix, regardless of the size of the input lists.