Elena' s AI Blog

Explainable AI with Python: SHAP and LIME for Model Transparency

10 Jul 2026 (updated: 17 Aug 2026) / 70 minutes to read

Elena Daehnhardt


Technical illustration unifying global and local explainability concepts using SHAP and LIME


TL;DR:
  • SHAP and LIME help explain model behaviour globally and locally so you can debug, govern, and trust predictions. All code uses the modern SHAP Explanation API and was verified against shap 0.49, lime 0.2, and scikit-learn 1.7.

πŸ“š This post is part of the "Applied ML & Model Engineering" series

Series: Applied ML & Model Engineering (Part 26 of 34)

  • Part 26: Explainable AI with Python: SHAP and LIME for Model Transparency

Previous: Part 26 β€” Explainable AI with Python: SHAP and LIME for Model Transparency

Introduction: Explainable AI with SHAP and LIME in Python

Black-box machine learning models can be difficult to trust and debug, especially in domains where decisions have significant consequences such as healthcare, finance, and criminal justice. A model that quietly denies someone a loan without anyone being able to say why is not just a technical problem β€” it is a governance problem.

Explainable AI (XAI) addresses this challenge by providing methods and tools to understand how AI models make decisions. Two of the most powerful and widely used libraries for model interpretability are SHAP (SHapley Additive exPlanations) and LIME (Local Interpretable Model-agnostic Explanations).

In this tutorial, I explore how to use SHAP and LIME in Python to make machine learning models transparent and interpretable. We will cover both global and local explanations, understand feature importance, and learn how to communicate model decisions effectively. Every code block below was executed and verified against shap 0.49.1, lime 0.2, scikit-learn 1.7.2, and xgboost 2.1.4 β€” worth mentioning because the SHAP API changed substantially around version 0.42, and a lot of older tutorials (including an earlier draft of this one) silently produce wrong results on current versions. I will point out those pitfalls as we go.

If you are new to the broader question of whether explainability is achievable at all, see my earlier post Explainable AI is possible for the conceptual groundwork this tutorial builds on.

Understanding Explainable AI

Why Explainability Matters

Explainable AI is crucial for several reasons:

  1. Trust and Adoption: Users are more likely to trust and adopt AI systems they can understand
  2. Regulatory Compliance: Many industries require explanations for AI decisions (the EU AI Act and GDPR’s provisions on automated decision-making being prominent examples)
  3. Debugging and Improvement: Understanding model behaviour helps identify and fix issues, such as a model latching onto a leaky feature
  4. Bias Detection: Explanations can reveal unfair biases in models before they reach production
  5. Stakeholder Communication: Clear explanations help communicate with non-technical stakeholders

Types of Model Explanations

Global Explanations

  • Explain the overall behaviour of the model
  • Show which features are most important across all predictions
  • Help understand model behaviour at a high level

Local Explanations

  • Explain individual predictions
  • Show why a specific prediction was made
  • Help understand model behaviour for specific cases

SHAP covers both: aggregate its per-prediction values and you get a global view. LIME is local by design, though we will later see SubmodularPick, its attempt at a global summary.

Setting Up the Environment

πŸ”’ Subscribe to keep reading.

Understanding SHAP (SHapley Additive exPlanations)

πŸ”’ Subscribe to keep reading.

Understanding LIME (Local Interpretable Model-agnostic Explanations)

πŸ”’ Subscribe to keep reading.

Advanced SHAP Analysis

πŸ”’ Subscribe to keep reading.

Advanced LIME Analysis

πŸ”’ Subscribe to keep reading.

Real-World Applications

πŸ”’ Subscribe to keep reading.

Best Practices for Explainable AI

πŸ”’ Subscribe to keep reading.

An Explainability Workflow

πŸ”’ Subscribe to keep reading.

Are These Methods Actually Used in Production?

πŸ”’ Subscribe to keep reading.

Conclusion: SHAP and LIME for Trustworthy Machine Learning

πŸ”’ Subscribe to keep reading.

References

πŸ”’ Subscribe to keep reading.

Subscribe to unlock the full article ❀️

The form below signs you up for the newsletter. It does not log you into the app β€” log in afterwards (same email) to unlock this article and download your subscriber gifts. New subscribers get an inbox mail: Set a password to unlock articles.

desktop bg dark

About Elena

Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.




Citation
Elena Daehnhardt. (2026) 'Explainable AI with Python: SHAP and LIME for Model Transparency', daehnhardt.com, 10 July 2026. Available at: https://daehnhardt.com/blog/2026/07/10/explainable-ai-python-shap-lime-model-transparency/
All Posts