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

How to Fine-Tune LLaMA 3.2-1B-Instruct for Korean Instruction Tasks with LoRA and Hugging Face

LLaMA 3.2-1B-Instruct is a lightweight instruction-tuned language model released by Meta. It is designed to handle a wide range of instruction-based tasks with relatively low computational resources. Although the model was trained with multilingual capabilities, its performance on languages not included in its training set—such as Korean—is limited. This tutorial demonstrates how to fine-tune this open-source model on a Korean dataset using Hugging Face Transformers and PEFT (specifically LoRA), enabling it to better respond to Korean instructions. 1. Prerequisites Before running the example code below, ensure you have the following libraries installed: pip install torch transformers datasets peft accelerate mlflow huggingface_hub To use the LLaMA model or KoAlpaca datasets, you'll need a Hugging Face token. Additionally, you may need to handle potential CUDA Out-Of-Memory (OOM) errors. The following code takes care of both: from huggingface_hub import login login("y...

RF-DETR: Overcoming the Limitations of DETR in Object Detection

RF-DETR (Region-Focused DETR), proposed in April 2025, is an advanced object detection architecture designed to overcome fundamental drawbacks of the original DETR (DEtection TRansformer) . In this technical article, we explore RF-DETR's contributions, architecture, and how it compares with both DETR and the improved model D-FINE . We also provide experimental benchmarks and discuss its real-world applicability. RF-DETR Architecture diagram for object detection Limitations of DETR DETR revolutionized object detection by leveraging the Transformer architecture, enabling end-to-end learning without anchor boxes or NMS (Non-Maximum Suppression). However, DETR has notable limitations: Slow convergence, requiring heavy data augmentation and long training schedules Degraded performance on low-resolution objects and complex scenes Lack of locality due to global self-attention mechanisms Key Innovations in RF-DETR RF-DETR intr...

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...