# SHAP

SHAP（SHapley Additive exPlanations，沙普利加性解释）是一种特征归因方法，它通过把每个特征分配到的贡献计算为合作博弈论中的[沙普利值](https://www.edgepedia.org/zh/shapley-value)，来解释单个机器学习预测。该方法由斯科特·伦德伯格（Scott Lundberg）和苏因·李（Su-In Lee）在 2017 年 NeurIPS 论文中提出，该论文识别出一类加性特征重要性度量，其中有一个唯一成员满足一组理想性质。<sup>[1](https://doi.org/10.48550/arxiv.1705.07874)</sup> 一篇 2023 年的综述统计了 24 种不同的沙普利归因估计算法，分为模型无关族与模型专属族。<sup>[2](https://www.nature.com/articles/s42256-023-00657-x)</sup> 对单个预测而言，各归因值精确相加等于预测值与基准值（模型在背景分布上的期望输出）之差。<sup>[3](https://shap.readthedocs.io/en/latest/generated/shap.KernelExplainer.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 模型的条件期望函数的沙普利值，\( f_{x}(z') = E[f(z) \mid z_{S}] \)<sup>[1](https://doi.org/10.48550/arxiv.1705.07874)</sup> |
| 公理 | 局部准确性、缺失性和一致性；扬（Young，1985）表明，沙普利值是唯一满足三条类似公理的值<sup>[1](https://doi.org/10.48550/arxiv.1705.07874)</sup> |
| 精确计算成本 | 对所有 \( 2^{M} \) 个联盟的加权求和；随特征数呈指数增长<sup>[4](https://doi.org/10.7717/peerj-cs.880)</sup> |
| 树集成 | 对 \( T \) 棵树、\( L \) 个叶节点、深度 \( D \) 的情形，可在 \( O(T \cdot L \cdot D^{2}) \) 时间内得到精确值<sup>[5](https://arxiv.org/abs/1706.06060)</sup> |
| Kernel SHAP 默认预算 | 每次预测进行 \( n_{\mathrm{samples}} = 2 \cdot p + 2048 \) 次模型评估<sup>[3](https://shap.readthedocs.io/en/latest/generated/shap.KernelExplainer.html)</sup> |
| 实用工具实际计算什么 | KernelSHAP 和 TreeSHAP 通常计算干预式 SHAP 值，而不是条件 SHAP 值<sup>[6](https://raw.githubusercontent.com/mlresearch/v258/main/assets/marzouk25a/marzouk25a.pdf)</sup> |
| 主要库 | shap Python 包，来自华盛顿大学（University of Washington）苏因·李的实验室和微软研究院（Microsoft Research）<sup>[7](https://github.com/shap/shap)</sup> |

## 工作原理

SHAP 把一次预测视为一个合作博弈。“参与者”是特征；给特征联盟 \( S \) 分配的值，是在 \( S \) 中的特征固定为正在解释的实例中的对应值、其余特征未知时，模型的期望输出：\( v(S) = E[f(x) \mid x_{S} = x^{*}_{S}] \)。<sup>[8](https://arxiv.org/pdf/1903.10464v3.pdf)</sup> 对特征 \( i \) 的归因就是它的沙普利值，即它相对于其他特征所有联盟的边际贡献的加权平均：

\[ \phi_{i} = \sum_{S \subseteq F \setminus \{i\}} \frac{|S|!\,(|F| - |S| - 1)!}{|F|!} \left[ f_{S \cup \{i\}}(x_{S \cup \{i\}}) - f_{S}(x_{S}) \right] \]

如原论文所述。<sup>[1](https://doi.org/10.48550/arxiv.1705.07874)</sup> 伦德伯格和李证明了沙普利核，

\[ \pi_{x'}(z') = \frac{M - 1}{\binom{M}{|z'|}\,|z'|\,(M - |z'|)} \]

在平方损失下，带该权重的线性回归可以恢复沙普利值；当 \( |z'| \in \{0, M\} \) 时，该核为无穷大，从而强制 \( \phi_{0} = f_{x}(\emptyset) \) 且 \( f(x) = \sum_{i=0}^{M} \phi_{i} \)。<sup>[1](https://doi.org/10.48550/arxiv.1705.07874)</sup> Kernel SHAP 使用的最小二乘形式中也出现同样的权重：\( \phi = (Z^{T} \cdot W \cdot Z)^{-1} \cdot Z^{T} \cdot W \cdot v \)，其中空联盟和大联盟的无穷权重被替换为一个大常数，例如 \( C = 10^{6} \)。<sup>[8](https://arxiv.org/pdf/1903.10464v3.pdf)</sup>

沙普利值的公理化依据建立在三条性质上：局部准确性（归因值加上基准值可重现预测）、缺失性（实例中不存在的特征得到零归因）和一致性（若模型改变使某特征的贡献上升，其归因不会下降）。<sup>[1](https://doi.org/10.48550/arxiv.1705.07874)</sup>

## 如何计算

精确计算要对 \( 2^{M} \) 个联盟求和，因此实际使用依赖模型专属捷径或采样。<sup>[4](https://doi.org/10.7717/peerj-cs.880)</sup>

**Kernel SHAP** 是模型无关的：它采样联盟，用背景数据集中的值替换缺失特征后评估模型，并求解上述沙普利核加权最小二乘问题。<sup>[3](https://shap.readthedocs.io/en/latest/generated/shap.KernelExplainer.html)</sup><sup> • </sup><sup>[8](https://arxiv.org/pdf/1903.10464v3.pdf)</sup> 默认预算为每次预测 \( 2 \cdot p + 2048 \) 次评估。<sup>[3](https://shap.readthedocs.io/en/latest/generated/shap.KernelExplainer.html)</sup>

**TreeSHAP** 通过沿每条树路径向下传播联盟统计量，在多项式时间 \( O(T \cdot L \cdot D^{2}) \) 内为树集成计算精确值，而不是朴素方法 \( O(T \cdot L \cdot 2^{M}) \)。<sup>[5](https://arxiv.org/abs/1706.06060)</sup> TreeExplainer 实现提供两种模式：'interventional'（干预式）需要背景数据集（建议 100 到 1000 个随机样本），其运行时间随数据集大小增长；'tree_path_dependent'（树路径依赖式）使用每条路径上的训练样本计数，不需要背景数据。<sup>[9](https://shap.readthedocs.io/en/latest/generated/shap.TreeExplainer.html)</sup> 还有一个 'approximate'（近似）选项，运行萨巴斯（Saabas）的单排序法，但该方法不具备沙普利值的一致性保证。<sup>[9](https://shap.readthedocs.io/en/latest/generated/shap.TreeExplainer.html)</sup>

**Deep SHAP** 通过将 DeepLIFT 乘子沿网络反向递归传递，利用沙普利值与 DeepLIFT 之间的联系，为神经网络近似归因。<sup>[1](https://doi.org/10.48550/arxiv.1705.07874)</sup><sup> • </sup><sup>[7](https://github.com/shap/shap)</sup> **Linear SHAP** 为具有独立特征的线性模型计算精确的解析值。<sup>[7](https://github.com/shap/shap)</sup> SHAP 交互值（主效应位于对角线上）可通过 TreeExplainer 对树模型精确计算。<sup>[7](https://github.com/shap/shap)</sup>

## 起源

SHAP 由伦德伯格和李在《解释模型预测的统一方法》（“A Unified Approach to Interpreting Model Predictions”，2017）中提出。<sup>[1](https://doi.org/10.48550/arxiv.1705.07874)</sup> 该论文统一了六种既有方法：LIME、沙普利回归值、沙普利采样值、定量输入影响（Quantitative Input Influence）、DeepLIFT 和逐层相关性传播。<sup>[1](https://doi.org/10.48550/arxiv.1705.07874)</sup> 其前身包括：LIME，由里贝罗（Ribeiro）、辛格（Singh）和格斯特林（Guestrin）在 KDD 2016 上提出，作为局部可解释模型无关解释<sup>[10](https://doi.org/10.48550/arxiv.1602.04938)</sup>；利波韦茨基（Lipovetsky）和康克林（Conklin，2001）的沙普利回归值<sup>[11](https://doi.org/10.1002/asmb.446)</sup>；以及埃里克·斯特伦贝尔（Erik Štrumbelj）和伊戈尔·科诺年科（Igor Kononenko，2010）对个体分类的博弈论解释，随后是他们在《知识与信息系统》（Knowledge and Information Systems，2013）上的沙普利采样值论文。<sup>[12](https://doi.org/10.1007/s10115-013-0679-x)</sup> DeepLIFT 本身由什里库马尔（Shrikumar）、格林赛德（Greenside）和昆达杰（Kundaje，2017）提出。<sup>[13](https://doi.org/10.48550/arxiv.1704.02685)</sup> 多项式时间树算法由伦德伯格、埃里昂（Erion）和李（2018）报告。<sup>[14](https://doi.org/10.48550/arxiv.1802.03888)</sup>

## 变体

模型专属加速包括杨继磊（Jilei Yang，2021）提出的 Fast TreeSHAP v1 和 v2，它们的运行速度分别约为 TreeSHAP 的 1.5 倍和 2.5 倍，其中 v2 的内存开销更高。<sup>[15](https://doi.org/10.48550/arxiv.2109.09847)</sup> 由罗里·米切尔（Rory Mitchell）、艾贝·弗兰克（Eibe Frank）和杰弗里·霍姆斯（Geoffrey Holmes，2022）提出的 GPUTreeShap，在单块 NVIDIA Tesla V100 GPU 上相对于 40 核 CPU 实现，对 SHAP 值最高加速 19 倍，对交互值最高加速 340 倍；它还把交互值的复杂度从 \( O(T \cdot L \cdot D^{2} \cdot M) \) 改善为 \( O(T \cdot L \cdot D^{3}) \)。<sup>[4](https://doi.org/10.7717/peerj-cs.880)</sup> 结构化数据变体 L-Shapley 和 C-Shapley 由陈（Chen）、宋（Song）、温赖特（Wainwright）和乔丹（Jordan，2018）提出。<sup>[16](https://doi.org/10.48550/arxiv.1808.02610)</sup> 图结构归因由沙普利流（Shapley flow；王（Wang）、温斯（Wiens）和伦德伯格（Lundberg），2020）处理<sup>[17](https://doi.org/10.48550/arxiv.2010.14592)</sup>；因果知识由因果沙普利值（赫斯克斯（Heskes）、西本（Sijben）、布库尔（Bucur）和克拉森（Claassen），2020）<sup>[18](https://doi.org/10.48550/arxiv.2011.01625)</sup> 以及非对称沙普利值（弗莱（Frye）、罗瓦特（Rowat）和费格（Feige），2019）处理。<sup>[19](https://doi.org/10.48550/arxiv.1910.06358)</sup>

在复杂度方面，对于类别选择采用加权投票或多数投票的随机森林和树集成分类器，精确 SHAP 分数是 #P-hard 的；对于可表示为（广义）d-DNNF 的分类器，包括具有离散特征的只读一次决策树，它们可在多项式时间内计算。<sup>[20](https://www.ijcai.org/proceedings/2024/0045.pdf)</sup> 一篇 2025 年 ICML 论文表明，在 HMM 建模的分布下，对于决策树、回归树集成、加权自动机以及线性回归，干预式 SHAP 和基线 SHAP 可在多项式时间内计算；而条件 SHAP 即使在经验分布或 HMM 分布下的决策树上仍然难以处理，两种价值函数之间存在严格的复杂度差距。<sup>[6](https://raw.githubusercontent.com/mlresearch/v258/main/assets/marzouk25a/marzouk25a.pdf)</sup>

## 应用

TreeSHAP 集成到 XGBoost 后，使具有数千棵树和数百个输入的模型能在不到一秒内得到解释。<sup>[5](https://arxiv.org/abs/1706.06060)</sup> FastSHAP 训练一个网络来实时估计沙普利值。<sup>[2](https://www.nature.com/articles/s42256-023-00657-x)</sup> 由休·陈（Hugh Chen）、斯科特·M·伦德伯格（Scott M. Lundberg）和苏因·李（Su-In Lee，2022）提出的 G-DeepSHAP，使用广义重缩放规则和多基线，比模型无关方法更快地解释线性、树和深度模型组成的流水线。<sup>[21](https://doi.org/10.1038/s41467-022-31384-3)</sup>

## 局限与替代方法

**相关特征与流形外评估。** 当特征相互依赖时，包括置换特征重要性、部分依赖图、LIME 和沙普利值在内的基于扰动的方法，会在训练数据很少或没有训练数据的区域评估模型，这可能产生误导，尤其是当模型依赖特征交互时。<sup>[22](https://link.springer.com/chapter/10.1007/978-3-031-04083-2_4)</sup> Kernel SHAP 假设特征独立，用边缘分布 \( p(x_{\bar{S}}) \) 替换条件分布 \( p(x_{\bar{S}} \mid x_{S}) \)。<sup>[8](https://arxiv.org/pdf/1903.10464v3.pdf)</sup> 阿斯（Aas）、尤勒姆（Jullum）和勒兰（Løland，2021）提出通过高斯、高斯 copula 和经验条件方法来估计条件分布，以纠正这一问题。<sup>[23](https://doi.org/10.1016/j.artint.2021.103502)</sup>

**用哪一种价值函数？** 扬青（Janzing）、米诺里克斯（Minorics）和布勒鲍姆（Blöbaum）认为，无条件期望而非条件期望才提供了“丢弃一个特征”的正确概念，这与 SHAP 包的理论依据相矛盾；他们还认为，试图把 SHAP“改进”为更好的条件近似在概念上是有缺陷的。<sup>[24](https://proceedings.mlr.press/v108/janzing20a/janzing20a.pdf)</sup> 库马尔（Kumar）、文卡塔苏布拉马尼安（Venkatasubramanian）、沙伊德格（Scheideeger）和弗里德勒（Friedler）表明，条件价值函数需要对特征相互关系建模，而干预式价值函数会引入分布外问题。<sup>[25](https://ar5iv.labs.arxiv.org/html/2002.11097)</sup> 这两种选择还会在其他性质上权衡：条件价值函数可能通过为模型并未使用的特征显示效应而违反敏感性，而边缘价值函数会在依赖关系下外推<sup>[22](https://link.springer.com/chapter/10.1007/978-3-031-04083-2_4)</sup>；条件沙普利值对对抗攻击具有鲁棒性，边缘沙普利值则不是。<sup>[26](https://link.springer.com/article/10.1007/s10618-024-01016-z)</sup> 叶（Yeh）、李（Lee）、刘（Liu）和[拉维库马尔](https://www.edgepedia.org/zh/ravikumar)（Ravikumar，2022）提出联合基线沙普利，以在流形内和流形外价值函数之间取得折中。<sup>[27](https://doi.org/10.48550/arxiv.2202.11919)</sup>

**唯一性与 Deep SHAP。** 孙达拉拉詹（Sundararajan）和纳吉米（Najmi）表明，多种操作化（基线沙普利、积分梯度、条件期望沙普利）以不同方式引用模型、训练数据和上下文，会给出非常不同的结果，并使唯一性结果无法跨变体适用。<sup>[28](https://proceedings.mlr.press/v119/sundararajan20b/sundararajan20b.pdf)</sup> 在 Deep SHAP 中逐层应用沙普利值会破坏公理保证，因为归因会变得对网络参数的排列方式敏感，而不是对计算出的函数敏感。<sup>[28](https://proceedings.mlr.press/v119/sundararajan20b/sundararajan20b.pdf)</sup> 原始 DeepSHAP 的单一平均基线是有偏的，因为干预式沙普利值可分解为单基线值的平均。<sup>[21](https://doi.org/10.1038/s41467-022-31384-3)</sup>

**相对于替代方法的定位。** LIME 围绕一次预测学习一个可解释的局部模型，但其启发式核不能恢复沙普利值，并且会违反局部准确性或一致性；在相似精度下，Kernel SHAP 比沙普利采样值需要更少的模型评估。<sup>[1](https://doi.org/10.48550/arxiv.1705.07874)</sup> 由孙达拉拉詹（Sundararajan）、塔利（Taly）和严（Yan，2017）提出的积分梯度，推广了奥曼-沙普利成本分摊方法，属于沙普利族操作化之一，而不是一个独立的公理化框架。<sup>[29](https://doi.org/10.48550/arxiv.1703.01365)</sup><sup> • </sup><sup>[28](https://proceedings.mlr.press/v119/sundararajan20b/sundararajan20b.pdf)</sup>

## References

1. [Lundberg, Scott, Lee, Su-In (2017). A Unified Approach to Interpreting Model Predictions. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1705.07874)
2. [Algorithms to estimate Shapley value feature attributions](https://www.nature.com/articles/s42256-023-00657-x)
3. [shap.KernelExplainer, official SHAP library documentation](https://shap.readthedocs.io/en/latest/generated/shap.KernelExplainer.html)
4. [Rory Mitchell, Eibe Frank, Geoffrey Holmes (2022). GPUTreeShap: massively parallel exact calculation of SHAP scores for tree ensembles. PeerJ Computer Science.](https://doi.org/10.7717/peerj-cs.880)
5. [Consistent feature attribution for tree ensembles (TreeSHAP paper)](https://arxiv.org/abs/1706.06060)
6. [On the Computational Tractability of the (Many) Shapley Values (Marzouk et al., ICML 2025)](https://raw.githubusercontent.com/mlresearch/v258/main/assets/marzouk25a/marzouk25a.pdf)
7. [shap/shap README (official repository)](https://github.com/shap/shap)
8. [Explaining individual predictions when features are dependent: more accurate approximations to Shapley values (Aas, Jullum, Løland)](https://arxiv.org/pdf/1903.10464v3.pdf)
9. [shap.TreeExplainer documentation](https://shap.readthedocs.io/en/latest/generated/shap.TreeExplainer.html)
10. [Ribeiro, Marco Tulio, Singh, Sameer, Guestrin, Carlos (2016). "Why Should I Trust You?": Explaining the Predictions of Any Classifier. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1602.04938)
11. [Stan Lipovetsky, Michael Conklin (2001). Analysis of regression in game theory approach. Applied Stochastic Models in Business and Industry.](https://doi.org/10.1002/asmb.446)
12. [Erik Štrumbelj, Igor Kononenko (2013). Explaining prediction models and individual predictions with feature contributions. Knowledge and Information Systems.](https://doi.org/10.1007/s10115-013-0679-x)
13. [Shrikumar, Avanti, Greenside, Peyton, Kundaje, Anshul (2017). Learning Important Features Through Propagating Activation Differences. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1704.02685)
14. [Lundberg, Scott M., Erion, Gabriel G., Lee, Su-In (2018). Consistent Individualized Feature Attribution for Tree Ensembles. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1802.03888)
15. [Yang, Jilei (2021). Fast TreeSHAP: Accelerating SHAP Value Computation for Trees. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2109.09847)
16. [Chen, Jianbo and colleagues (2018). L-Shapley and C-Shapley: Efficient Model Interpretation for Structured Data. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1808.02610)
17. [Wang, Jiaxuan, Wiens, Jenna, Lundberg, Scott (2020). Shapley Flow: A Graph-based Approach to Interpreting Model Predictions. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2010.14592)
18. [Heskes, Tom and colleagues (2020). Causal Shapley Values: Exploiting Causal Knowledge to Explain Individual Predictions of Complex Models. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2011.01625)
19. [Frye, Christopher, Rowat, Colin, Feige, Ilya (2019). Asymmetric Shapley values: incorporating causal knowledge into model-agnostic explainability. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1910.06358)
20. [Updates on the Complexity of SHAP Scores (IJCAI 2024)](https://www.ijcai.org/proceedings/2024/0045.pdf)
21. [Hugh Chen, Scott M. Lundberg, Su-In Lee (2022). Explaining a series of models by propagating Shapley values. Nature Communications.](https://doi.org/10.1038/s41467-022-31384-3)
22. [General Pitfalls of Model-Agnostic Interpretation Methods for Machine Learning Models (Springer, 2023)](https://link.springer.com/chapter/10.1007/978-3-031-04083-2_4)
23. [Kjersti Aas, Martin Jullum, Anders Løland (2021). Explaining individual predictions when features are dependent: More accurate approximations to Shapley values. Artificial Intelligence.](https://doi.org/10.1016/j.artint.2021.103502)
24. [Feature relevance quantification in explainable AI: A causal problem (Janzing et al., AISTATS 2020)](https://proceedings.mlr.press/v108/janzing20a/janzing20a.pdf)
25. [Problems with Shapley-value-based explanations as feature importance measures (Kumar et al., ICML 2020)](https://ar5iv.labs.arxiv.org/html/2002.11097)
26. [A comparative study of methods for estimating model-agnostic Shapley value explanations (Data Mining and Knowledge Discovery, 2024)](https://link.springer.com/article/10.1007/s10618-024-01016-z)
27. [Yeh, Chih-Kuan and colleagues (2022). Threading the Needle of On and Off-Manifold Value Functions for Shapley Explanations. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2202.11919)
28. [The Many Shapley Values for Model Explanation (Sundararajan & Najmi, ICML 2020)](https://proceedings.mlr.press/v119/sundararajan20b/sundararajan20b.pdf)
29. [Sundararajan, Mukund, Taly, Ankur, Yan, Qiqi (2017). Axiomatic Attribution for Deep Networks. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1703.01365)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Machine learning and neural computation › Machine learning methods

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/shap

English original: https://www.edgepedia.org/shap
