Explaining the loss of a model can be very useful for debugging and model monitoring. This notebook gives a very simple example of how this works. Note that explaining the loss of a model requires passing the labels, and is only supported for the feature_dependence="independent" option of TreeExplainer.
This notebook will be fleshed out once we post a full write-up of this method.
In [1]:
import shap
import sklearn
import xgboost
import numpy as np
In [2]:
X,y = shap.datasets.adult()
model = xgboost.XGBClassifier()
model.fit(X,y)
# compute the logistic log-loss
model_loss = -np.log(model.predict_proba(X)[:,1]) * y + -np.log(model.predict_proba(X)[:,0]) * (1-y)
model_loss[:10]
Out[2]:
In [3]:
explainer = shap.TreeExplainer(model, X, feature_dependence="independent", model_output="logloss")
explainer.shap_values(X.iloc[:10,:], y[:10]).sum(1) + np.array([explainer.expected_value(v) for v in y[:10]])
Out[3]: