Example Code
Let’s consider an example of how to use confusion matrix in Python. We will use the scikit-learn library to create a confusion matrix.
from sklearn.metrics import confusion_matrix # Actual Values y_true = [1, 0, 0, 1, 1, 0, 1] # Predicted Values y_pred = [1, 0, 1, 1, 0, 0, 1] # Create a confusion matrix confusion_matrix(y_true, y_pred)
Output :
array([[2, 1], [1, 3]])
The output shows a 2×2 confusion matrix with 2 true positives, 1 false positive, 1 false negative, and 3 true negatives.