Understanding the Confusion Matrix (with Python Example

A confusion matrix is a table that is used to evaluate the performance of a classification algorithm.

What is a Confusion Matrix?

The confusion matrix is a summary of prediction results on a classification problem. The number of correct and incorrect predictions are summarized with count values and broken down by each class. This helps to understand which classes are being misclassified and improve the performance of the model.

How to Interpret a Confusion Matrix

A confusion matrix is a table with two rows and two columns, as well as additional rows and columns for each class. The rows represent the actual class, while the columns represent the predicted class. The cells in the table contain the number of predictions made by the classification algorithm for each combination of actual and predicted classes.

Here’s an example of a confusion matrix for a binary classification problem:

Predicted Positive Predicted Negative
Actual Positive True Positive (TP) False Negative (FN)
Actual Negative False Positive (FP) True Negative (TN)

In this table, the True Positives (TP) are the cases where the model correctly predicted the positive class. The False Negatives (FN) are the cases where the model predicted the negative class, but the actual class was positive. The False Positives (FP) are the cases where the model predicted the positive class, but the actual class was negative. Finally, the True Negatives (TN) are the cases where the model correctly predicted the negative class.

Calculating Evaluation Metrics

There are several evaluation metrics that can be calculated from a confusion matrix. These include:

  • Accuracy: The overall accuracy of the model, which is the number of correct predictions divided by the total number of predictions. It is calculated as: (TP + TN) / (TP + TN + FP + FN)
  • Precision: The precision of the model, which is the number of true positives divided by the total number of predicted positives. It is calculated as: TP / (TP + FP)
  • Recall: The recall of the model, which is the number of true positives divided by the total number of actual positives. It is calculated as: TP / (TP + FN)
  • F1 Score: The F1 score is the harmonic mean of precision and recall. It is calculated as: 2 * (precision * recall) / (precision + recall)

Examples of Confusion Matrices in Python

Here is an example of how to calculate evaluation metrics from a confusion matrix in Python using the scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html">confusion_matrix function from the scikit-learn library:


from sklearn.metrics import confusion_matrix

y_true = [1, 0, 1, 1, 0, 1]
y_pred = [0, 0, 1, 1, 0, 1]

confusion_matrix(y_true, y_pred)

Relevant entities

Entity Property
True positive Prediction is positive and the actual outcome is positive
True negative Prediction is negative and the actual outcome is negative
False positive Prediction is positive but the actual outcome is negative
False negative Prediction is negative but the actual outcome is positive
Accuracy Proportion of correct predictions in the population
Precision Proportion of correct positive predictions in the population
Recall Proportion of actual positive cases that were correctly predicted

Frequently asked questions

What is a confusion matrix?

A confusion matrix is a table that compares the predicted values of a model with the true values of a dataset.

What are the rows and columns in a confusion matrix?

A confusion matrix has two rows and two columns. The rows represent the true values of the dataset, and the columns represent the predicted values of the model.

How do you calculate the accuracy of a model using a confusion matrix?

Accuracy is calculated by dividing the number of correct predictions by the total number of predictions. This can be calculated by adding the number of true positives and true negatives and dividing by the total number of predictions.

What are the limitations of using a confusion matrix?

A confusion matrix is only effective for comparing binary classification models. It cannot be used for multi-class classification or regression problems.

Conclusion

A confusion matrix is a powerful tool for evaluating the performance of a classification algorithm. It allows us to visualize the number of true positive, true negative, false positive, and false negative predictions made by the model, as well as calculate important evaluation metrics such as accuracy, precision, recall, and F1 score. By analyzing the results of a confusion matrix, we can better understand the strengths and weaknesses of a model and make informed decisions about how to improve its performance.