Confusion Matrix Practice Problem
This data science coding problem helps you practice Evaluation Metrics, confusion matrix, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Evaluation Metrics.
- Problem ID: 44
- Problem key: 44-confusion-matrix
- URL: https://datacrack.app/solve/44-confusion-matrix
- Difficulty: easy
- Topic: Evaluation Metrics
- Module: Introduction to Machine Learning
Problem Statement
# 🧩 Confusion Matrix
---
### 🎯 Goal
Understand how to evaluate classification predictions by constructing the confusion matrix from model outputs.
---
### 📥 Input / 📤 Output
**Input:**
* `y_true`: list of true labels (0 or 1)
* `y_pred`: list of predicted labels (0 or 1)
**Output:**
* A 2x2 confusion matrix in the format:
```
[[TN, FP],
[FN, TP]]
```
---
### 💻 Task Description
1️⃣ Compute the number of:
* True Positives (TP)
* True Negatives (TN)
* False Positives (FP)
* False Negatives (FN)
2️⃣ Construct the confusion matrix using:
```
[[TN, FP],
[FN, TP]]
```
3️⃣ Return the confusion matrix
---
### 🧩 Starter Code
```python
def confusion_matrix(y_true, y_pred):
"""
Computes the confusion matrix.
Args:
y_true (list): True labels (0 or 1)
y_pred (list): Predicted labels (0 or 1)
Returns:
list: 2x2 confusion matrix [[TN, FP], [FN, TP]]
"""
# Your code here
pass
```
---
### 💡 Example
```python
y_true = [1, 0, 1, 1, 0]
y_pred = [1, 0, 0, 1, 0]
confusion_matrix(y_true, y_pred)
```
Expected Output:
```
[[2, 0],
[1, 2]]
```
---
Starter Code
def confusion_matrix(y_true, y_pred):
"""
Computes the confusion matrix.
Args:
y_true (list): True labels (0 or 1)
y_pred (list): Predicted labels (0 or 1)
Returns:
list: 2x2 confusion matrix [[TN, FP], [FN, TP]]
"""
# Your code here
pass