# Explainability with SHAP and LIME

> Global and local explanations of model behaviour, from permutation importance and partial dependence to SHAP values and LIME. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/machine-learning/explainability
Last updated: 2026-11-28

Global and local explanations of model behaviour, from permutation importance and partial dependence to SHAP values and LIME.

Intermediate · about 6 hours · difficulty 3/5

Learn first: [Gradient Boosting with XGBoost and LightGBM](https://www.internhack.in/roadmaps/tracks/machine-learning/gradient-boosting), [Evaluation Metrics](https://www.internhack.in/roadmaps/tracks/machine-learning/evaluation-metrics)

Stakeholders, regulators and your own debugging all need to know why a model predicts what it does. Explainability tools answer two different questions: how the model behaves overall (**global**) and why it made this particular prediction (**local**).

### Global methods

- **Coefficients** of linear models, if features are scaled and not strongly correlated.
- **Impurity-based importance** from trees is fast but biased towards high-cardinality features.
- **Permutation importance**: shuffle one feature on validation data and measure how much the score drops. Model-agnostic and more trustworthy.
- **Partial dependence and ICE plots** show how predictions change as one feature varies.

### Local methods

- **SHAP** assigns each feature a contribution based on Shapley values from game theory; contributions add up to the difference between the prediction and the average prediction. `TreeExplainer` is exact and fast for gradient boosting; waterfall, beeswarm and dependence plots are the standard views.
- **LIME** fits a simple surrogate model around one prediction by perturbing the input.
- For deep models: **integrated gradients**, Grad-CAM for images, and attention visualizations (with caution).

### Caveats

Explanations describe the model, not the real world; they do not imply causation. Correlated features split credit in confusing ways. Explanations can also be manipulated, so use them as evidence alongside other checks, and prefer inherently interpretable models when the stakes are high and accuracy allows.

**Good enough to move on** when you can produce global and local SHAP explanations for a gradient boosting model, spot a leaky feature with them, and explain their limits to a non-technical audience.

## Key concepts

- **Global vs local explanations**
- **Permutation importance**
- **Partial dependence and ICE plots**
- **SHAP values and plots**
- **LIME**
- **Integrated gradients**
- **Limits of explanations**

## Mini project

Train a LightGBM model on a tabular dataset, compare impurity, permutation and SHAP importance, produce waterfall plots for three individual predictions, and write a one-page explanation for a business reader.

## Check yourself

Why can impurity-based feature importance mislead, and what does it mean that SHAP values are additive?

## Free resources

- [Interpretable Machine Learning](https://christophm.github.io/interpretable-ml-book/) (Christoph Molnar)
- [SHAP documentation](https://shap.readthedocs.io/) (SHAP)
- [scikit-learn: inspection](https://scikit-learn.org/stable/inspection.html) (scikit-learn)

Previous: [Cloud ML Platforms](https://www.internhack.in/roadmaps/tracks/machine-learning/cloud-ml-platforms) · Next: [Fairness and Bias in ML](https://www.internhack.in/roadmaps/tracks/machine-learning/fairness)

## More in Responsible and Trustworthy ML

- [Explainability with SHAP and LIME](https://www.internhack.in/roadmaps/tracks/machine-learning/explainability)
- [Fairness and Bias in ML](https://www.internhack.in/roadmaps/tracks/machine-learning/fairness)
- [Privacy and Security in ML](https://www.internhack.in/roadmaps/tracks/machine-learning/privacy-and-security)
- [Model Cards, Datasheets and Governance](https://www.internhack.in/roadmaps/tracks/machine-learning/model-documentation)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Machine Learning Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/machine-learning)
