Model Interpretability — SHAP and LIME
Explain any individual prediction. Global feature importance, local SHAP explanations, LIME for black-box models, and presenting model decisions to regulators.
Your loan rejection model has AUC = 0.94. The customer calls asking why their loan was rejected. "The model said so" is not a legal answer in India.
RBI's guidelines on algorithmic lending require that credit decisions be explainable to applicants. SEBI requires explanation of algorithmic trading decisions. Healthcare regulations require that diagnostic AI justify its conclusions. The EU AI Act mandates explanations for high-risk AI systems. Interpretability is not optional in regulated industries — it is a legal requirement.
But even outside regulation, interpretability matters for trust. A data scientist at CRED who cannot explain why the model rejected a specific applicant cannot debug the model when it makes systematic errors. Cannot detect bias. Cannot improve it. The model is a black box that produces outputs nobody understands — including the people responsible for it.
This module covers two complementary techniques. SHAP (SHapley Additive exPlanations) computes the exact contribution of each feature to each prediction using game theory — it is mathematically rigorous and model-agnostic. LIME (Local Interpretable Model-agnostic Explanations) fits a simple interpretable model in the local neighbourhood of a prediction — faster and more flexible but less rigorous. Together they cover the full range of interpretability needs in production.
A cricket team wins a match. How much credit does each player deserve? You cannot just look at the final score — you need to figure out each player's contribution. SHAP uses Shapley values from cooperative game theory: simulate all possible team subsets, measure how much the score changes when each player joins. Average across all subsets. That average is each player's fair credit.
SHAP does the same for model features. Simulate all possible feature subsets, measure how much the prediction changes when each feature is added. Average across all subsets. That average is each feature's fair contribution to this specific prediction.
Built-in, permutation, and SHAP — what each measures and when each misleads
Before SHAP, there were two common approaches to feature importance. Both have significant limitations that SHAP fixes. Understanding why they fail makes SHAP's value obvious.
Counts how many times each feature is used to split nodes, weighted by the improvement in the split criterion. Available in sklearn, XGBoost, LightGBM via .feature_importances_.
Heavily biased toward high-cardinality features. A feature with 1,000 unique values will be used in more splits than a binary feature even if both have equal predictive power. Tells you about the model structure, not about the data.
Quick sanity check only. Never use for regulatory reporting.
For each feature, randomly shuffle its values and measure how much the model performance drops. A large drop = the feature is important. No drop = the model ignores it. Available via sklearn.inspection.permutation_importance.
When two features are correlated (e.g. income and loan_amount), shuffling one breaks the correlation — the model appears to rely less on each than it actually does. Correlated features share importance between them rather than reflecting true individual contributions.
Better than split-based for final model analysis. But misleads on correlated features.
Computes the exact marginal contribution of each feature to each individual prediction using Shapley values from cooperative game theory. Mathematically proven to be the only attribution method satisfying four key fairness axioms.
Slower than built-in importance. KernelExplainer is very slow on large datasets. TreeExplainer is fast but tree-model-only.
Use for all production reporting, regulatory compliance, and debugging. The gold standard.
SHAP values — from global importance to individual explanations
SHAP computes two levels of explanation simultaneously. Global SHAP importance(mean |SHAP| across all predictions) tells you which features matter most for the model overall — comparable to feature importance but more reliable. Local SHAP valuesexplain one specific prediction — which features pushed this particular applicant's default probability up or down, and by how much.
Shapley proved in 1951 that there exists exactly one attribution satisfying all four axioms. SHAP implements that attribution. No other feature importance method satisfies all four simultaneously.
Three SHAP explainers — which one to use for which model
SHAP has different explainers optimised for different model types. TreeExplainer is exact and fast for tree models. LinearExplainer is exact for linear models. KernelExplainer works for any model but is slow. DeepExplainer works for neural networks.
LIME — explain any prediction by fitting a local simple model
LIME takes a fundamentally different approach to SHAP. Instead of computing exact Shapley values, it asks: what simple model (linear regression or decision tree) best approximates the complex model's behaviour in the immediate neighbourhood of this prediction? That simple model's coefficients are the explanation.
LIME generates synthetic samples near the prediction point, gets the complex model's predictions for all of them, then fits a weighted linear model where samples closer to the original point are weighted more heavily. The linear model's coefficients tell you which features pushed the prediction up or down locally.
Production explanation pipeline — CRED loan rejection letters
At CRED, when a loan application is rejected the system must generate a plain-English explanation that satisfies RBI guidelines. The explanation must name the specific factors that led to rejection, not just say "algorithmic decision." Here is the complete pipeline.
Every common interpretability mistake — explained and fixed
The Evaluation section is complete. Section 7 — Deep Learning — begins next.
You have now completed the full Model Evaluation section: evaluation metrics, calibration, ROC curves, cross-validation, hyperparameter tuning, and interpretability. You can build, evaluate, tune, calibrate, and explain any classical ML model.
Section 7 — Deep Learning — begins with neural networks. Everything changes: instead of hand-crafted features, the model learns its own representations. Instead of gradient boosting on tabular data, you train multi-layer networks on images, sequences, and text. Module 40 builds a neural network from scratch — forward pass, backpropagation, and gradient descent — before introducing PyTorch.
Forward pass, backpropagation, and gradient descent — built from NumPy before touching PyTorch. The foundation every deep learning framework is built on.
🎯 Key Takeaways
- ✓Interpretability is a legal requirement in regulated industries — RBI, SEBI, and EU AI Act all mandate that algorithmic decisions be explainable. "The model said so" is not acceptable. SHAP and LIME provide the explanation infrastructure.
- ✓Three types of feature importance, in order of reliability: built-in split-based (biased toward high-cardinality features), permutation importance (misleads on correlated features), and SHAP (mathematically proven correct — the only attribution satisfying all four fairness axioms). Always prefer SHAP for production reporting.
- ✓SHAP computes two levels simultaneously: global importance (mean |SHAP| across all predictions — reliable feature ranking) and local importance (individual SHAP values per prediction — which features drove this specific outcome and by how much).
- ✓Choose the right SHAP explainer: TreeExplainer for XGBoost/LightGBM/RF (fast, exact), LinearExplainer for logistic/linear regression (fastest, exact), KernelExplainer for any model including SVM and neural nets (slow, approximate). Always pass the underlying model, not a Pipeline wrapper.
- ✓LIME fits a local linear model in the neighbourhood of each prediction. It is faster than KernelSHAP for non-tree models and produces intuitive rule-based explanations. But it is stochastic — different runs give different results. Use a fixed random_state and high num_samples. Prefer SHAP when determinism matters.
- ✓For production loan or credit decisions: pre-compute the SHAP explainer once at startup, store it, and reuse it for all requests. Generate explanations in plain English using a feature description dictionary that maps technical feature names to human-readable phrases. Always report the top 3 risk factors — more than 3 overwhelms the applicant.
Discussion
0Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.