|
| 1 | +--- |
| 2 | +title: "LIME & SHAP: Interpreting the Black Box" |
| 3 | +sidebar_label: LIME & SHAP |
| 4 | +description: "A deep dive into local explanations using LIME and game-theory-based global/local explanations with SHAP." |
| 5 | +tags: [machine-learning, xai, lime, shap, interpretability] |
| 6 | +--- |
| 7 | + |
| 8 | +When using complex models like XGBoost, Random Forests, or Neural Networks, we often need to know: *"Why did the model deny this specific loan?"* or *"What features are driving the predictions for all users?"* **LIME** and **SHAP** are the industry standards for answering these questions. |
| 9 | + |
| 10 | +## 1. LIME (Local Interpretable Model-Agnostic Explanations) |
| 11 | + |
| 12 | +LIME works on the principle that while a model may be incredibly complex globally, it can be approximated by a simple, linear model **locally** around a specific data point. |
| 13 | + |
| 14 | +### How LIME Works: |
| 15 | +1. **Select a point:** Choose the specific instance you want to explain. |
| 16 | +2. **Perturb the data:** Create new "fake" samples by slightly varying the features of that point. |
| 17 | +3. **Get predictions:** Run these fake samples through the "black box" model. |
| 18 | +4. **Weight the samples:** Give more weight to samples that are closer to the original point. |
| 19 | +5. **Train a surrogate:** Train a simple Linear Regression model on this weighted, perturbed dataset. |
| 20 | +6. **Explain:** The coefficients of the linear model act as the explanation. |
| 21 | + |
| 22 | +## 2. SHAP (SHapley Additive exPlanations) |
| 23 | + |
| 24 | +SHAP is based on **Shapley Values** from cooperative game theory. It treats every feature of a model as a "player" in a game where the "payout" is the model's prediction. |
| 25 | + |
| 26 | +### The Core Concept: |
| 27 | +SHAP calculates the contribution of each feature by comparing what the model predicts **with** the feature versus **without** it, across all possible combinations of features. |
| 28 | + |
| 29 | +**The result is "Additive":** |
| 30 | + |
| 31 | +$$ |
| 32 | +f(x) = \text{base\_value} + \sum \text{shap\_values} |
| 33 | +$$ |
| 34 | + |
| 35 | +* **Base Value:** The average prediction of the model across the training set. |
| 36 | +* **SHAP Value:** The amount a specific feature pushed the prediction higher or lower than the average. |
| 37 | + |
| 38 | +## 3. LIME vs. SHAP: The Comparison |
| 39 | + |
| 40 | +| Feature | LIME | SHAP | |
| 41 | +| :--- | :--- | :--- | |
| 42 | +| **Foundation** | Local Linear Surrogates | Game Theory (Shapley Values) | |
| 43 | +| **Consistency** | Can be unstable (results vary on perturbation) | Mathematically consistent and fair | |
| 44 | +| **Speed** | Very Fast | Can be slow (computationally expensive) | |
| 45 | +| **Scope** | Strictly Local | Both Local and Global | |
| 46 | + |
| 47 | +## 4. Visualization Logic |
| 48 | + |
| 49 | +The following diagram illustrates the flow of SHAP from the model output down to the feature-level contribution. |
| 50 | + |
| 51 | +```mermaid |
| 52 | +graph TD |
| 53 | + Output[Model Prediction: 0.85] --> Base[Base Value / Mean: 0.50] |
| 54 | + |
| 55 | + subgraph Feature_Contributions [Shapley Attribution] |
| 56 | + F1[Income: +0.20] |
| 57 | + F2[Credit Score: +0.10] |
| 58 | + F3[Age: +0.10] |
| 59 | + F4[Debt: -0.05] |
| 60 | + end |
| 61 | + |
| 62 | + Base --> F1 |
| 63 | + F1 --> F2 |
| 64 | + F2 --> F3 |
| 65 | + F3 --> F4 |
| 66 | + F4 --> Final[Final Prediction: 0.85] |
| 67 | + |
| 68 | + style F1 fill:#e8f5e9,stroke:#2e7d32,color:#333 |
| 69 | + style F4 fill:#ffebee,stroke:#c62828,color:#333 |
| 70 | + style Final fill:#e1f5fe,stroke:#01579b,color:#333 |
| 71 | +
|
| 72 | +``` |
| 73 | + |
| 74 | +## 5. Global Interpretation with SHAP |
| 75 | + |
| 76 | +One of SHAP's greatest strengths is the **Summary Plot**. By aggregating the SHAP values of all points in a dataset, we can see: |
| 77 | + |
| 78 | +1. Which features are the most important. |
| 79 | +2. How the *value* of a feature (high vs. low) impacts the output. |
| 80 | + |
| 81 | +## 6. Implementation Sketch (Python) |
| 82 | + |
| 83 | +Both LIME and SHAP have robust Python libraries. |
| 84 | + |
| 85 | +```python |
| 86 | +import shap |
| 87 | +import lime |
| 88 | +import lime.lime_tabular |
| 89 | + |
| 90 | +# --- SHAP Example --- |
| 91 | +explainer = shap.TreeExplainer(my_model) |
| 92 | +shap_values = explainer.shap_values(X_test) |
| 93 | + |
| 94 | +# Visualize the first prediction's explanation |
| 95 | +shap.initjs() |
| 96 | +shap.force_plot(explainer.expected_value, shap_values[0,:], X_test.iloc[0,:]) |
| 97 | + |
| 98 | +# --- LIME Example --- |
| 99 | +explainer_lime = lime.lime_tabular.LimeTabularExplainer( |
| 100 | + X_train.values, feature_names=X_train.columns, class_names=['Negative', 'Positive'] |
| 101 | +) |
| 102 | +exp = explainer_lime.explain_instance(X_test.values[0], my_model.predict_proba) |
| 103 | +exp.show_in_notebook() |
| 104 | + |
| 105 | +``` |
| 106 | + |
| 107 | +## References |
| 108 | + |
| 109 | +* **SHAP GitHub:** [Official Repository and Documentation](https://github.com/slundberg/shap) |
| 110 | +* **LIME Paper:** ["Why Should I Trust You?": Explaining the Predictions of Any Classifier](https://arxiv.org/abs/1602.04938) |
| 111 | +* **Distill.pub:** [Interpretable Machine Learning Guide](https://christophm.github.io/interpretable-ml-book/shap.html) |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +**LIME and SHAP help us understand "Tabular" and "Text" data. But what if we are using CNNs for images? How do we know which pixels the model is looking at?** |
0 commit comments