100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Model Explainability (SHAP/LIME) Cheat Sheet

Model Explainability (SHAP/LIME) Cheat Sheet

Interpret black-box model predictions using SHAP Shapley values and LIME local surrogate models, with plots and feature attribution.

2 PagesIntermediateFeb 14, 2026

SHAP TreeExplainer

Compute Shapley values efficiently for tree-based models like XGBoost or LightGBM.

python
import shapimport xgboost as xgbmodel = xgb.XGBClassifier().fit(X_train, y_train)explainer = shap.TreeExplainer(model)shap_values = explainer.shap_values(X_test)# force plot for a single predictionshap.force_plot(explainer.expected_value, shap_values[0], X_test.iloc[0])

Global Feature Importance Plots

Summarize feature impact across the whole test set with beeswarm and bar plots.

python
shap.summary_plot(shap_values, X_test)  # beeswarm: direction + magnitude per featureshap.summary_plot(shap_values, X_test, plot_type="bar")  # mean |SHAP value| ranking# dependence plot: how one feature's value affects its SHAP contributionshap.dependence_plot("age", shap_values, X_test)

Model-Agnostic Explanations with KernelExplainer

Explain any black-box model (e.g. an sklearn pipeline) using sampled background data.

python
background = shap.sample(X_train, 100)explainer = shap.KernelExplainer(model.predict_proba, background)shap_values = explainer.shap_values(X_test.iloc[:20], nsamples=200)shap.summary_plot(shap_values[1], X_test.iloc[:20])  # class 1

Local Explanation with LIME

Explain a single prediction by fitting an interpretable local surrogate model.

python
from lime.lime_tabular import LimeTabularExplainerexplainer = LimeTabularExplainer(    X_train.values,    feature_names=X_train.columns.tolist(),    class_names=["no_churn", "churn"],    mode="classification",)exp = explainer.explain_instance(    X_test.iloc[0].values, model.predict_proba, num_features=8)exp.show_in_notebook()print(exp.as_list())

SHAP vs LIME

When to reach for each explainability approach.

  • SHAP- theoretically grounded (Shapley values), consistent global + local attributions
  • TreeExplainer- exact, fast SHAP values for tree ensembles specifically
  • KernelExplainer- model-agnostic but slow; approximates SHAP via weighted sampling
  • LIME- fast local surrogate, easier to reason about but less stable across runs
  • Additivity property- SHAP values sum to (prediction - baseline), which LIME does not guarantee
Pro Tip

Use TreeExplainer whenever the model is tree-based — it computes exact SHAP values in polynomial time, whereas KernelExplainer only approximates them and can give unstable results run-to-run on the same input.

Was this cheat sheet helpful?

Explore Topics

#ModelExplainabilitySHAPLIME#ModelExplainabilitySHAPLIMECheatSheet#DataScience#Intermediate#SHAPTreeExplainer#Global#Feature#Importance#MachineLearning#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet