Generate a Confusion Matrix for Binary Classification

Easy
MLE Interview Prep

Task: Generate a Confusion Matrix

Your task is to implement the function confusion_matrix(data) that generates a confusion matrix for a binary classification problem. The confusion matrix provides a summary of the prediction results on a classification problem, allowing you to visualize how many data points were correctly or incorrectly labeled.

Input:

  • A list of lists, where each inner list represents a pair
  • [y_true, y_pred] for one observation. y_true is the actual label, and y_pred is the predicted label.

Output:

  • A 2×22 \times 2 confusion matrix represented as a list of lists.

Examples

Example 1:
Input: data = [[1, 1], [1, 0], [0, 1], [0, 0], [0, 1]] print(confusion_matrix(data))
Output: [[1, 1], [2, 1]]
Explanation: The confusion matrix shows the counts of true positives, false negatives, false positives, and true negatives.

Starter Code


from collections import Counter

def confusion_matrix(data):
	# Implement the function here
	pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews