objectif
Illustrer la décomposition d’une prédiction via waterfall/force.
code minimal
import shap
import numpy as np
from xgboost import XGBClassifier
from sklearn.datasets import load_breast_cancer
X, y = load_breast_cancer(return_X_y=True)
model = XGBClassifier(n_estimators=50, tree_method="hist", random_state=0).fit(X, y)
expl = shap.TreeExplainer(model)
sv = expl.shap_values(X[:1])
base = expl.expected_value
# shap.plots.waterfall(shap.Explanation(values=sv[0], base_values=base, data=X[:1][0]))
print(len(sv[0]) == X.shape[1])
utilisation
# Force plot (inline HTML si Jupyter)
# shap.force_plot(base, sv[0], X[:1][0])
print(True)
variante(s) utile(s)
# Summary bar
# shap.plots.bar(shap.Explanation(values=sv[:100], base_values=base, data=X[:100]))
print(True)
notes
- Les tracés nécessitent un environnement graphique; en scripts, sauvegardez en fichier avec matplotlib.pyplot.savefig.