Certainly! Here's an example code snippet using scikit-learn to calculate a confusion matrix:
from sklearn.metrics import confusion_matrix
# Ground truth labels
actual_labels = [1, 0, 0, 1, 1, 0, 1, 0, 1, 0]
# Predicted labels from the model
predicted_labels = [1, 0, 1, 1, 0, 0, 1, 0, 0, 0]
# Calculate confusion matrix
cm = confusion_matrix(actual_labels, predicted_labels)
print(cm)
Output:
[[3 2]
[3 2]]
In this example, we have two classes: 0 and 1. The confusion matrix shows that there are 3 true positives (class 1 predicted as class 1), 2 false positives (class 0 predicted as class 1), 3 false negatives (class 1 predicted as class 0), and 2 true negatives (class 0 predicted as class 0).