使用自定义 masker (掩码器)
这里我们展示了如何为任何 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])

有更多有用的示例的想法吗? 欢迎提交 Pull Request 来为此文档笔记本做出贡献!