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

Building an MCP Agent with UV, Python & mcp-use

Model Context Protocol (MCP) is an open protocol designed to enable AI agents to interact with external tools and data in a standardized way. MCP is composed of three components: server , client , and host . MCP host The MCP host acts as the interface between the user and the agent   (such as Claude Desktop or IDE) and plays the role of connecting to external tools or data through MCP clients and servers. Previously, Anthropic’s Claude Desktop was introduced as a host, but it required a separate desktop app, license, and API key management, leading to dependency on the Claude ecosystem.   mcp-use is an open-source Python/Node package that connects LangChain LLMs (e.g., GPT-4, Claude, Groq) to MCP servers in just six lines of code, eliminating dependencies and supporting multi-server and multi-model setups. MCP Client The MCP client manages the MCP protocol within the host and is responsible for connecting to MCP servers that provide the necessary functions for the ...

How to Save and Retrieve a Vector Database using LangChain, FAISS, and Gemini Embeddings

How to Save and Retrieve a Vector Database using LangChain, FAISS, and Gemini Embeddings Efficient storage and retrieval of vector databases is foundational for building intelligent retrieval-augmented generation (RAG) systems using large language models (LLMs). In this guide, we’ll walk through a professional-grade Python implementation that utilizes LangChain with FAISS and Google Gemini Embeddings to store document embeddings and retrieve similar information. This setup is highly suitable for advanced machine learning (ML) and deep learning (DL) engineers who work with semantic search and retrieval pipelines. Why Vector Databases Matter in LLM Applications Traditional keyword-based search systems fall short when it comes to understanding semantic meaning. Vector databases store high-dimensional embeddings of text data, allowing for approximate nearest-neighbor (ANN) searches based on semantic similarity. These capabilities are critical in applications like: Question Ans...

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