A local explanation of a single prediction with LIME
Finding out why the model classified a particular sample.
Goal: Finding the most influential features globally and for a single sample.
import numpy as np, shap, xgboost as xgb
from sklearn.datasets import load_diabetes
data = load_diabetes()
model = xgb.XGBRegressor(n_estimators=100, max_depth=3, random_state=0).fit(data.data, data.target)
explainer = shap.TreeExplainer(model)
sv = explainer.shap_values(data.data)
imp = np.abs(sv).mean(0)
print("Top features:", [data.feature_names[i] for i in imp.argsort()[::-1][:3]])
i = 0
print(f"Sample 0: base {explainer.expected_value:.2f} + sum of SHAP {sv[i].sum():.2f} = {explainer.expected_value + sv[i].sum():.2f}")
print(f"Model prediction: {model.predict(data.data[:1])[0]:.2f}")
TreeExplainer computes Shapley values exactly and quickly for tree models. The mean absolute value of each feature gives global importance, and the sum of a sample's values plus the base value equals the model's output for it (the additivity property).
Top features: ['s5', 'bmi', 'bp'] Sample 0: base 152.14 + sum of SHAP 42.02 = 194.15 Model prediction: 194.15
This example was actually run and tested on 21 November 2026 on a CPU — shap 0.54.1, xgboost 3.4.1, scikit-learn 1.9.1, python 3.13.18
Finding out why the model classified a particular sample.
Attributing the network's prediction to input features and checking the completeness axiom.
Showing the regions the network relied on for its decision.
Sections
No section has this name. Press “Search” to search all site content.
Favourites
My results & certificates
My academy progressMy quiz resultsKids’ progressAcademy certificatesQuiz certificatesStart any lesson or quiz and it will show up here so you can get back to it quickly.
Settings
I am the AI Frameworks & Libraries Assistant. Describe your project or ask me about a library, a comparison or a code example, and I will answer from the encyclopedia's database with links and sources.
Answers from the encyclopedia's database only, without a language model and without sending your question to any third party.
The assistant can make mistakes, so check the attached sources.