使用自定义掩码器
这里我们展示了如何为任何 SHAP 模型不可知解释方法提供自定义的 Masker。掩码通常是领域相关的,因此考虑超越 SHAP 默认包含的扰动数据的其他方法通常是有帮助的。
[1]:
import xgboost
import shap
# train XGBoost model
X, y = shap.datasets.adult()
model = xgboost.XGBClassifier().fit(X.values, y)
# A masking function takes a binary mask vector as the first argument and
# the model arguments for a single sample after that
# It returns a masked version of the input x, where you can return multiple
# rows to average over a distribution of masking types
def custom_masker(mask, x):
# in this simple example we just zero out the features we are masking
return (x * mask).reshape(1, len(x))
# compute SHAP values
explainer = shap.Explainer(model.predict_proba, custom_masker)
shap_values = explainer(X[:100])
# plot the SHAP values for the positive class
shap.plots.beeswarm(shap_values[..., 1])
有更多有用示例的想法吗?我们鼓励提交增加此文档笔记本的拉取请求!