Skip to main content

Understanding Accuracy, Precision, Recall, and F1 Score in ML/DL Models

When developing a machine learning or deep learning model, it's critical to know how well the model performs. This requires more than intuition—it needs measurable evaluation metrics. Accuracy alone is often insufficient, especially in imbalanced datasets where the majority class dominates.

Consider a cancer diagnosis model. If 99% of patients are healthy, a model predicting everyone as healthy achieves 99% accuracy. However, it fails to detect actual patients, rendering it ineffective. This is where metrics like Precision, Recall, and F1 Score become invaluable.

1. Understanding the Confusion Matrix

Evaluation metrics are derived from the confusion matrix, which summarizes prediction outcomes:

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

These four values form the foundation of most classification metrics.

2. Accuracy

Definition: The proportion of correct predictions
Formula: $\frac{\textbf{(TP+TN)}}{\textbf{(TP+TN+FP+FN)}}$

Accuracy is easy to understand but becomes misleading when the dataset is imbalanced. For instance, predicting all emails as non-spam in a 95% non-spam dataset yields 95% accuracy but fails the actual purpose.

3. Precision

Definition: Among predicted positives, how many are truly positive
Formula: $\frac{\textbf{(TP)}}{\textbf{(TP+FP)}}$

Precision evaluates false alarms. It’s crucial when false positives are costly—like in spam detection, fraud detection, or medical screening where a false alarm might create unnecessary anxiety.

Example: If 20 emails are predicted as spam and 18 are indeed spam, precision is 18/20 = 90%.

4. Recall (Sensitivity)

Definition: Among actual positives, how many are correctly identified
Formula: $\frac{\textbf{(TP)}}{\textbf{(TP+FN)}}$

Recall measures the ability to catch all positives. In healthcare, missing a disease is often more dangerous than raising a false alarm, so high recall is preferred.

Example: Out of 50 actual cancer patients, if 45 are predicted correctly, recall is 45/50 = 90%.

5. F1 Score

Definition: Harmonic mean of precision and recall
Formula: $2*\frac{\textbf{(Precision*Recall)}}{\textbf{(Precision+Recall)}}$

F1 Score balances precision and recall. It's low if either precision or recall is low. It is especially useful in imbalanced datasets.

Example: With precision = 80% and recall = 60%, the F1 Score = 2*(0.8*0.6)/(0.8+0.6) ≈ 68.6%.

6. Metrics and Class Imbalance

When classes are balanced, most metrics perform reliably. However, with imbalance (e.g., 95:5 ratio), accuracy may be misleading:

  • High accuracy might come from majority class prediction only
  • Precision alone ignores missed positives (FN)
  • Recall alone may allow too many false positives (FP)
  • F1 Score ensures balanced performance by considering both FP and FN

7. Multi-class Classification

For multi-class problems, precision, recall, and F1 score are computed per class and aggregated:

  • Macro average: Unweighted mean across classes
  • Micro average: Global count-based averaging
  • Weighted average: Weighted by support (number of instances per class)

In class-imbalanced scenarios, weighted averaging is often more representative.

8. Conclusion & Recommended Resources

Metrics like precision, recall, and F1 score offer deeper insight into model performance than accuracy alone. Choosing the right metric depends on your domain and the nature of the classification task.

Recommended References:

Comments

Popular

Understanding SentencePiece: A Language-Independent Tokenizer for AI Engineers

In the realm of Natural Language Processing (NLP), tokenization plays a pivotal role in preparing text data for machine learning models. Traditional tokenization methods often rely on language-specific rules and pre-tokenized inputs, which can be limiting when dealing with diverse languages and scripts. Enter SentencePiece—a language-independent tokenizer and detokenizer designed to address these challenges and streamline the preprocessing pipeline for neural text processing systems. What is SentencePiece? SentencePiece is an open-source tokenizer and detokenizer developed by Google, tailored for neural-based text processing tasks such as Neural Machine Translation (NMT). Unlike conventional tokenizers that depend on whitespace and language-specific rules, SentencePiece treats the input text as a raw byte sequence, enabling it to process languages without explicit word boundaries, such as Japanese, Chinese, and Korean. This approach allows SentencePiece to train subword models di...

Mastering the Byte Pair Encoding (BPE) Tokenizer for NLP and LLMs

Byte Pair Encoding (BPE) is one of the most important and widely adopted subword tokenization algorithms in modern Natural Language Processing (NLP), especially in training Large Language Models (LLMs) like GPT. This guide provides a deep technical dive into how BPE works, compares it with other tokenizers like WordPiece and SentencePiece, and explains its practical implementation with Python code. This article is optimized for AI engineers building real-world models and systems. 1. What is Byte Pair Encoding? BPE was originally introduced as a data compression algorithm by Gage in 1994. It replaces the most frequent pair of bytes in a sequence with a single, unused byte. In 2015, Sennrich et al. adapted BPE for NLP to address the out-of-vocabulary (OOV) problem in neural machine translation. Instead of working with full words, BPE decomposes them into subword units that can be recombined to represent rare or unseen words. 2. Why Tokenization Matters in LLMs Tokenization is th...

Using Gemini API in LangChain: Step-by-Step Tutorial

What is LangChain and Why Use It? LangChain  is an open-source framework that simplifies the use of  Large Language Models (LLMs)  like OpenAI, Gemini (Google), and others by adding structure, tools, and memory to help build real-world applications such as chatbots, assistants, agents, or AI-enhanced software. Why Use LangChain for LLM Projects? Chainable Components : Easily build pipelines combining prompts, LLMs, tools, and memory. Multi-Model Support : Work with Gemini, OpenAI, Anthropic, Hugging Face, etc. Built-in Templates : Manage prompts more effectively. Supports Multi-Turn Chat : Manage complex interactions with memory and roles. Tool and API Integration : Let the model interact with external APIs or functions. Let's Walk Through the Code: Gemini + LangChain I will break the code into  4 main parts , each showcasing different features of LangChain and Gemini API. Part 1: Basic Gemini API Call Using LangChain import os from dotenv import load_dotenv load_dot...