The four values above in the confusion matrix diagram are used to calculate important classification metrics such as accuracy, precision, recall, and F1 score. Let’s explore each of them in detail.
Build practical AI and software engineering skills with the Executive Post Graduate Certificate in AI-Native Software Engineering. Learn to use AI across the software development lifecycle and prepare for the evolving demands of modern engineering roles.
Understanding the Four Parts of a Confusion Matrix
Every confusion matrix machine learning for a binary classification problem is made from the four counts which are TP, TN, FP, FN. Let’s explore each of them in detail.
1. True Positive (TP)
A True Positive (TP) is a case where the model predicts the positive class and the actual outcome is also positive.
For example, if an email is actually spam and the model also predicts it as spam, the result is a True Positive.
2. True Negative (TN)
A True Negative is a case when the model predicts the negative class and the outcome is also negative.
For example, if a mail is not spam and the model also predicted the mail as not spam, the result is True Negative.
Also Read: Linear Regression in Machine Learning: Types, Algorithm, and Implementation
3. False Positive (FP)
A False Positive is a case where the model predicts the positive class but the result or outcome is negative. This is also called a Type I error.
For example, if a normal mail is predicted as spam mail, this will be a False Positive.
4. False Negative (FN)
A False is a case where the model predicts the negative class but the result or outcome is positive. This is also called a Type II error.
For example, if a spam email is incorrectly classified as normal email, this will be a False negative.
The idea of a confusion matrix began during World War II. It was linked to Signal Detection Theory (SDT), developed in the 1940s. Radar operators had to identify whether a signal was an enemy aircraft or something harmless, such as birds. A False Positive meant detecting a threat that was not there, while a False Negative meant missing a real threat. This idea later became the foundation of the confusion matrix used in machine learning.
Also Read: K Means Clustering in Machine Learning: Algorithm, Steps, Examples, and Applications
Once you know about TP, TN, FP, and FN, you can calculate several important metrics, which make up the confusion matrix formula set most commonly used to evaluate a classification model. Let’s explore those metrics.
Confusion Matrix Metrics
Common metrics are used in confusion matrix in machine learning including accuracy, precision, recall, F1 score and Specificity. Let’s explore each of them in detail.
1. Accuracy
Accuracy measures the proportion of total predictions that the model classified correctly. A higher accuracy means the model has correctly classified a larger proportion of the observations.
Confusion Matrix Formula for Accuracy:
Accuracy = (TP + TN) / (TP + TN + FP + FN)
2. Precision
Precision measures how many of the observations predicted as positive were actually positive. It is useful when False Positives are costly.
Confusion Matrix Formula for Precision:
Precision = TP / (TP + FP)
Also Read: Logistic Regression in Machine Learning: Algorithm, Types, Examples, and Applications
3. Recall
Recall measures how many of the actual positive cases were correctly identified by the model. It is especially important when missing a positive case can have serious consequences.
Confusion Matrix Formula for Recall:
Recall = TP / (TP + FN)
4. F1 Score
The F1 score combines precision and recall into a single metric. It is calculated as their harmonic mean. The F1 score is useful when you want to consider both precision and recall together.
Confusion Matrix Formula for F1 Score:
F1 Score = 2 × (Precision × Recall) / (Precision + Recall)
5. Specificity
Specificity measures how well a model identifies actual negative cases. A high specificity means the model correctly identifies a large proportion of negative cases.
Confusion Matrix Formula for Specificity:
Specificity = TN / (TN + FP)
Now let's explore how a confusion matrix is created and interpreted after a classification model makes predictions.
Also Read: Clustering in Machine Learning: Types, Algorithms, Examples, and How It Works
How Does a Confusion Matrix in Machine Learning Work?
A confusion matrix machine learning compares each prediction made by a model with the actual answer and groups the results into four categories.
The process commonly follows these steps:
Train a classification model using the available dataset.
Use the trained model to make predictions on test data.
Compare the predicted labels with the actual labels.
Count the True Positives, True Negatives, False Positives, and False Negatives.
Place these values into the confusion matrix.
Use the values to calculate metrics such as accuracy, precision, recall, and F1 score.
For example, suppose a model predicts whether 100 customers will leave a company or not. The actual customer outcomes can be compared with the model's predictions. The resulting confusion matrix can show whether the model is correctly identifying customers who are likely to leave and customers who are likely to stay.
Also Read: Supervised vs Unsupervised Learning: Differences, Examples, and Applications
Now let's see a simple confusion matrix example to understand how its values and metrics are calculated which helps you to understand the topic more clearly.
Confusion Matrix Example
Through an actual confusion matrix example you can make the concepts far more clear than formulas alone.
Simple Binary Classification Example
Suppose a machine learning model predicts whether patients have a particular condition or not. After testing the model on 100 patients, the results are:
Actual / Predicted | Positive | Negative |
Positive | 40 | 10 |
Negative | 5 | 45 |
Here:
TP = 40
FN = 10
FP = 5
TN = 45
How to Calculate TP, TN, FP, and FN
From the matrix:
True Positive = 40: 40 positive cases were correctly predicted.
True Negative = 45: 45 negative cases were correctly predicted.
False Positive = 5: 5 negative cases were incorrectly predicted as positive.
False Negative = 10: 10 positive cases were incorrectly predicted as negative.
Now we can use these values to calculate the model's metrics by using the confusion matrix formulas we discussed above.
1. Accuracy: (TP + TN) / (TP + TN + FP + FN)
(40 + 45) / (40 + 45 + 5 + 10) = 0.85
So, the model has an accuracy of 85%.
2. Precision: TP / (TP + FP)
40 / (40 + 5) = 0.889
The precision is approximately 88.9%.
3. Recall: TP / (TP + FN)
40 / (40 + 10) = 0.80
The recall is 80%.
4. F1 Score: 2 × (Precision × Recall) / (Precision + Recall)
2 × (0.889 × 0.80) / (0.889 + 0.80) ≈ 0.842
The F1 score is approximately 84.2%.
How to Interpret the Results?
The results show that the model correctly identified most positive and negative cases. Its precision of about 88.9% means that most cases predicted as positive were actually positive.
The recall of 80% means the model identified 80% of the actual positive cases. The difference between precision and recall also shows why looking at only accuracy may not provide a complete picture of model performance.
Also Read: Support Vector Machine (SVM) in Machine Learning: Algorithm, Types, Kernels, and Examples
Now let's see how a confusion matrix in machine learning works when a model has more than two classes.
Confusion Matrix for Multiclass Classification
A confusion matrix can also be used for multiclass classification problems. Instead of two rows and two columns, it contains one row and one column for each class.
For example, a model classifying images into Cat, Dog, and Bird could have a matrix like this:
Actual / Predicted | Cat | Dog | Bird |
Cat | 45 | 3 | 2 |
Dog | 4 | 42 | 4 |
Bird | 1 | 5 | 44 |
The diagonal values represent correct predictions, while the values outside the diagonal represent incorrect predictions.
For example, the value 45 means 45 actual cats were correctly classified as cats. The value 3 means three actual cats were incorrectly classified as dogs.
Now, try using the confusion matrix above to solve this exercise yourself which gives you a better understanding of how confusion matrix machine learning works.
Exercise: Analyze the Confusion Matrix
Try solving this new example yourself.
Suppose a model predicts whether emails are Spam or Not Spam. The model is tested on 200 emails, with the following results:
Actual / Predicted | Positive | Negative |
Spam | 70 | 15 |
Not Spam | 10 | 105 |
Using this confusion matrix, calculate:
True Positive (TP)
True Negative (TN)
False Positive (FP)
False Negative (FN)
Accuracy
Precision
Recall
F1 Score
Tip: First identify TP, TN, FP, and FN from the matrix. Then use the formulas to calculate each metric.
Once you complete the above exercise let's understand how to create a confusion matrix in Python using scikit-learn.
Strengthen your data science skills with the Executive Post Graduate Certificate in Applied Data Science & Agentic AI. Learn to work with data, AI, and intelligent agents to build practical solutions for real-world business challenges.
How to Create a Confusion Matrix in Python
In the section you will get a complete confusion matrix python example using scikit-learn to help you implement the confusion matrix machine learning practically.
1. Calculate the Confusion Matrix Using Scikit-learn
The confusion_matrix() function from sklearn.metrics can calculate the matrix from actual and predicted labels.
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
actual = [1, 1, 1, 0, 0, 0, 1, 0]
predicted = [1, 1, 0, 0, 0, 1, 1, 0]
cm = confusion_matrix(actual, predicted)
print(cm)
Output:
[[3 1]
[1 3]]
2. Visualise the Confusion Matrix as a Heatmap
You can also visualise the confusion matrix as a heatmap:
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues")
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.title("Confusion Matrix")
plt.show()
Output: