How to read a confusion matrix

In this recipe, we will learn how to interpret a confusion matrix. We will learn all the terminologies associated with a confusion matrix.

How to read a confusion matrix?

A confusion matrix is a table that shows how well a classification model performs on the test data. A confusion matrix is fairly simple to understand but let us get acquainted with a few terminologies first. Consider the confusion matrix given below-



It is a confusion matrix for binary classification. Let us assume that YES stands for person testing positive for a disease and NO stands for a person not testing positive for the disease. Therefore, there are two predicted classes – YES and NO.

150 people were tested for the disease. The classifier predicted 105 people to have tested positive and the rest 45 as negative. However, in actuality, 110 people tested positive and 40 tested negative.

Terminologies –

True Positive (TP) -> Observations that were predicted YES and were actually YES.
True Negative (TN) -> Observations that were predicted NO and were actually NO.
False Positive (FP) -> Observations that were predicted YES but were actually NO.
False Negative (FN) -> Observations that were predicted NO but were actually YES.
Accuracy -> It is the measure of how correctly was the classifier able to predict.
Error Rate -> It is the measure of how incorrect was the classifier.
True Positive Rate or Recall (TPR) -> It is the measure of, how often does the classifier predicts YES when it is actually YES.
False Positive Rate (FPR) -> It is the measure of, how often does the classifier predicts YES when it is NO.
True Negative Rate (TNR) -> It is the measure of, how often does the classifier predicts NO when it is actually NO.
Precision -> TP/(TP+FP) i.e. True positives divided by predicted YES
Recall -> TP/(TP+FN) i.e. True positives divided by actual YES
F beta score -> ((1+beta2) * Precision * Recall) / (beta2 * Precision + Recall) (0.5, 1 and 2 are common values of beta)


Let us calculate the above-mentioned measures for our confusion matrix.
From the table,

TP = 100
FP = 5
TN = 35
FN = 10
Accuracy = (TP + TN) / (TP+TN+PF+FN) = (100+35)/150 = 0.9
Error Rate = (FP + FN) / (TP+TN+PF+FN) = (5+10)/150 = 0.1
TPR = TP/actual YES = 100/110 = 0.9090
FPR = FP/actual no = 5/40 = 0.125
TNR = TN/actual no = 35/40 = 0.875
Precision = TP/predicted YES = 100/105 =0.952
This is how you can derive conclusions from a confusion matrix.

To learn how to build a confusion matrix you can refer to the following tutorials –

How to build a confusion matrix in R
How to get Classification Confusion Matrix?
Classification report and Confusion matrix in python

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Llama2 Project for MetaData Generation using FAISS and RAGs
In this LLM Llama2 Project, you will automate metadata generation using Llama2, RAGs, and AWS to reduce manual efforts.

Build Multi Class Text Classification Models with RNN and LSTM
In this Deep Learning Project, you will use the customer complaints data about consumer financial products to build multi-class text classification models using RNN and LSTM.

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Build PowerBI Dashboard for Water Quality Sensor Data Analysis
In this PowerBI Project, you will learn to build a PowerBI Dashboard to analyze and visualize water quality sensor data from various European countries.

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

Build an AI Insurance Agent for Eligibility Analysis Using CrewAI
Build an AI Insurance Agent that automates eligibility checks by extracting medical details, mapping conditions to policy terms, and generating explainable coverage decisions using CrewAI and LLMs. This is an upcoming project that is expected to be launched in June.

OSZAR »