shap
它能做什么
SHAP 可以帮你理解 AI 模型是如何做出决策的。它能告诉你,输入信息中的哪些部分对最终的预测结果最重要。
适用场景
- ✓当你想知道模型为什么会对某件事做出预测时。
- ✓当你想比较不同因素在某个预测中的重要程度时。
- ✓当你想检查模型是否在使用正确的信息时。
技能链接
https://github.com/K-Dense-AI/scientific-agent-skills/blob/main/skills/shap/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请解释一下,为什么模型预测这位客户会购买产品 X。”
每周 5 个新技能,1 分钟读完。
工作原理
SHAP 会把每个特征(也就是输入信息)与平均值进行比较,计算它对预测结果的影响。它会为每个特征打一个分数,表明该特征是提高还是降低了预测的概率。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
SHAP
Use SHAP to describe how a fitted predictive model maps inputs to outputs. Work from the modern shap.Explanation API, make the explained output and background distribution explicit, and validate every explanation before interpreting it.
This skill is aligned with SHAP 0.52.0 (released 2026-05-28). That release requires Python 3.12 or newer.
The maintained examples were checked on Python 3.12.10 with SHAP 0.52.0, NumPy 2.5.3, pandas 3.0.6, scikit-learn 1.9.1, and matplotlib 3.11.2. Native tests cover small tree, exact, permutation, partition, linear, additive, kernel, text, and constant-image games; additional numeric XGBoost 3.4.1 smoke checks cover raw, probability, loss, and interaction outputs. Reference snippets that require a project model, framework, or data are adaptation templates; optional pretrained/deep, GPU, and distributed integrations remain illustrative and require their own runtime validation.
Operating Rules
- Explain a fixed, evaluated model; do not use SHAP as a substitute for predictive validation.
- Use held-out or clearly labeled analysis rows for explanations. Choose background rows only from an appropriate training or reference population.
- State the explained output: regression value, raw margin, probability, log loss, logit, or another model method.
- Prefer
shap.Explanationobjects andexplainer(X). Some specialized options still require.shap_values(X), including deep ranked outputs, gradient sampling budgets, and Kernel SHAPnsamples. Preserve their output indexes and baselines explicitly. - For multi-output models, select one output before using tabular plots:
explanation[..., output_index]. - Check
base_values + values.sum(...)against the exact model output being explained. - Treat SHAP as a description of model behavior under a masking/background choice. It does not establish causality, fairness, recourse, or scientific mechanism.
- Never silence an additivity failure until input shape, preprocessing, model version, output space, and row ordering have been checked.
- Do not load untrusted pickle, joblib, model, or explainer artifacts; those formats can execute code during deserialization.
Install
Create an isolated environment and pin the documented release:
uv venv --python 3.12
source .venv/bin/activate
uv pip install "shap[plots]==0.52.0"
shap[plots] installs the plotting dependencies. Add the fitted model's package at a version compatible with the project. For older Python compatibility, read references/migration.md instead of silently installing a different SHAP release.
Confirm the environment before debugging an API mismatch:
import platform
import shap
print("Python:", platform.python_version())
print("SHAP:", shap.__version__)
Standard Workflow
1. Define the explanation target
Record:
- model and preprocessing version;
- exact callable or model method being explained;
- output name/index and units;
- evaluation rows;
- background/reference population;
- masker and explainer algorithm;
- SHAP and model-library versions.
For classifiers, decide whether the task needs raw margins or probabilities. Defaults differ by model family; never infer units from the plot color or sign.
2. Select an explainer and masker
Start with shap.Explainer(model, masker) when automatic dispatch is sufficient. Instantiate a specialized explainer when its assumptions or output controls matter.
| Situation | Preferred choice | Important constraint |
|---|---|---|
| Supported tree ensemble | TreeExplainer |
model_output="probability" and "log_loss" require interventional masking and background data |
| Linear model | LinearExplainer |
The masker determines interventional versus correlation-aware behavior |
| Small feature space | ExactExplainer |
Cost grows quickly with unconstrained feature count |
| General tabular callable | PermutationExplainer |
Budget at least one full forward/reverse permutation |
| Hierarchical feature groups, text, or image | PartitionExplainer |
The partition tree changes the cooperative game |
| Differentiable neural network | DeepExplainer or GradientExplainer |
Framework support, output shape, and background choice require testing |
| Legacy Kernel SHAP workflow | KernelExplainer |
Usually much slower than model-specific methods |
…
常见问题
SHAP 能证明模型是正确的吗?
不能。SHAP 只能解释模型的行为,不能验证预测本身是否正确,也不能证明因果关系。
我可以把 SHAP 用在任何 AI 模型上吗?
SHAP 支持多种类型的模型,尤其是机器学习模型,但具体的设置方式会有所不同。
'explainer' 和 'masker' 是什么意思?
'explainer'(解释器)是用来计算重要性的方法,'masker'(掩码器)则用来为这些比较定义一个'背景'或'参照'。
详情
作者的原始描述
Explain and audit machine-learning predictions with SHAP. Use for selecting SHAP explainers and maskers, computing and validating feature attributions, handling multi-output explanations, and producing local or global SHAP visualizations.
热度
- GitHub 星标
- 51k
- 排名
- #3573
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …