Explaining a tree model with SHAP values
Finding the most influential features globally and for a single sample.
Goal: Finding out why the model classified a particular sample.
from lime.lime_tabular import LimeTabularExplainer
from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import RandomForestClassifier
data = load_breast_cancer()
clf = RandomForestClassifier(n_estimators=100, random_state=0).fit(data.data, data.target)
exp = LimeTabularExplainer(data.data, feature_names=list(data.feature_names),
class_names=list(data.target_names), random_state=0)
e = exp.explain_instance(data.data[0], clf.predict_proba, num_features=3)
print("Predicted class:", data.target_names[clf.predict(data.data[:1])[0]])
for cond, w in e.as_list():
print(f"{w:+.3f} {cond}")
LIME creates perturbed samples around the sample, observes how the prediction changes, then fits a simple local linear model. The weights express the effect of each condition on the decision for this sample only. The explanation changes slightly between runs because of random sampling, so we fixed random_state.
Predicted class: malignant -0.128 worst perimeter > 125.40 -0.122 worst concave points > 0.16 -0.119 worst radius > 18.79
This example was actually run and tested on 21 November 2026 on a CPU — lime 0.2.0.1, scikit-learn 1.9.1, python 3.13.18
Finding the most influential features globally and for a single 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.