♎
Limited AI
  • Machine Learning
    • Linear Model Cheating Sheet
    • Nonlinear Model Cheating Sheet
    • General Linear Model 1
    • General Linear Model 2
    • General Linear Model 3
    • Tree Based Methods
    • Tree Based Methods Supplement
    • XG,Cat,Light__Boosting
    • KNN&PCA
    • Model Performance
    • Model Evaluation
    • Code Practice
      • KNN
      • Decision Tree Python Code
    • Data and Feature Engineering
      • Handle Bias Data
      • Cold Start Problem
  • Deep Learning
    • Summary v2
    • Basic Neural Network
      • From Linear to Deep
      • Perceptron and Activation Function
      • NN network Details
      • Backpropagation Details
      • Gradient Vanishing vs Gradient Exploding
    • Basic CNN
      • Why CNN
      • Filter/ Convolution Kernel and Its Operation
      • Padding& Stride
      • Layers
      • Extra:From Fully Connected Layers to Convolutions
      • Extra: Multiple Input and Multiple Output Channels
    • Advance CNN
      • Convolutional Neural Networks(LeNet)
      • Deep Convolution Neural Networks(AlexNet)
      • Networks Using Blocks (VGG)
      • Network in Network(NiN)
      • Multi-Branch Networks(GoogLeNet&I mageNet)
      • Residual Networks(ResNet) and ResNeXt
      • Densely Connected Networks(DenseNet)
      • Batch Normalization
    • Basic RNN
      • Seq Model
      • Raw Text to Seq
      • Language Models
      • Recurrent Neural Networks(RNN)
      • Backpropagation Through Time
    • Advance RNN
      • Gated Recurrent Units(GRU)
      • Long Short-Term Memory(LSTM)
      • Bidirectional Recurrent Neural Networks(BRNN)
      • Encoder-Decoder Architecture
      • Seuqence to Sequence Learning(Seq2Seq)
    • Attention Mechanisms and Transformers
      • Queries, Keys, and Values
      • Attention is all you need
        • Attention and Kernel
        • Attention Scoring Functions
        • The Bahdanau Attention Mechanism
        • Multi-Head Attention
        • Self-Attention
        • Attention的实现
      • The Transformer Architecture
        • Extra Reading
        • 最短的最大路径长度
      • Large-Scaling Pretraning with Transformers
        • BERT vs OpenAI GPT vs ELMo
        • Decoder Model框架
        • Bert vs XLNet
        • T5& GPT& Bert比较
        • 编码器-解码器架构 vs GPT 模型
        • Encoder vs Decoder Reference
      • Transformers for Vision
      • Transformer for Multiomodal
    • NLP Pretraining
      • Word Embedding(word2vec)
        • Extra Reading
      • Approximate Training
      • Word Embedding with Global Vectors(GloVe)
        • Extra Reading
        • Supplement
      • Encoder(BERT)
        • BERT
        • Extra Reading
      • Decoder(GPT&XLNet&Lamma)
        • GPT
        • XLNet
          • XLNet架构
          • XLNet特点与其他比较
      • Encoder-Decoder(BART& T5)
        • BART
        • T5
  • GenAI
    • Introduction
      • GenAI Paper Must Read
      • GenAI六个阶段
    • Language Models Pre-training
      • Encoder-Decoder Architecture
      • Encoder Deep Dive
      • Decoder Deep Dive
      • Encoder VS Decoder
      • Attention Mechanism
      • Transformers
    • Example: Llama 3 8B架构
    • Fine-Tuning Generation Models
    • RAG and Adavance RAG
    • AI Agent
  • Statistics and Optimization
    • A/B testing
    • Sampling/ABtesting/GradientMethod
    • Gradient Decent Deep Dive
  • Machine Learning System Design
    • Extra Reading
    • Introduction
  • Responsible AI
    • AI Risk and Uncertainty
      • What is AI risk
      • General Intro for Uncertainty Quantification
      • Calibration
      • Conformal Prediction
        • Review the linear regression
        • Exchangeability
        • Split Conformal Prediction
        • Conformalized Quantile Regression
        • Beyond marginal coverage
        • Split Conformal Classification
        • Full Conformal Coverage
        • Cross-Validation +
        • Conformal Histgram Regression
    • xAI
      • SHAP value
  • Extra Research
    • Paper Reading
    • Reference
Powered by GitBook
On this page
  • Raising a question
  • What is baseline?
  • What is the SHAP additive?
  • SHAP value VS linear coefficient
  • Extra
  1. Responsible AI
  2. xAI

SHAP value

Raising a question

We are interested in understanding how each feature contributes to a prediction. In linear models, this is relatively straightforward to compute. Consider a linear model:

f^(x)=β0+β1x1+⋯+βpxp\hat{f}(x) = \beta_0 + \beta_1 x_1 + \cdots + \beta_p x_pf^​(x)=β0​+β1​x1​+⋯+βp​xp​

where xxx is the input instance we want to explain, each xjx_jxj​ for ( j=1,…,pj = 1, \ldots, pj=1,…,p ) is the feature value, and ( βj\beta_jβj​ ) is the coefficient corresponding to feature jjj.

The contribution of the jjj-th feature to the prediction ( f^(x)\hat{f}(x)f^​(x) ) is defined as:

ϕj=βjxj−E(βjXj)=βjxj−βjE(Xj)\phi_j = \beta_j x_j - \mathbb{E}(\beta_j X_j) = \beta_j x_j - \beta_j \mathbb{E}(X_j)ϕj​=βj​xj​−E(βj​Xj​)=βj​xj​−βj​E(Xj​)

Here, ( E(βjXj\mathbb{E}(\beta_j X_jE(βj​Xj​) denotes the average contribution of the jjj-th feature. The contribution measures how much this feature deviates from its average impact. If we sum the contributions of all features for a given instance, we obtain:

∑j=1pϕj=∑j=1p(βjxj−E(βjXj))=(β0+∑j=1pβjxj)−(β0+∑j=1pE(βjXj))=f^(x)−E(f(X))\sum_{j=1}^p \phi_j = \sum_{j=1}^p (\beta_j x_j - \mathbb{E}(\beta_j X_j)) \\ = \left( \beta_0 + \sum_{j=1}^p \beta_j x_j \right) - \left( \beta_0 + \sum_{j=1}^p \mathbb{E}(\beta_j X_j) \right) \\ = \hat{f}(x) - \mathbb{E}(f(X))j=1∑p​ϕj​=j=1∑p​(βj​xj​−E(βj​Xj​))=(β0​+j=1∑p​βj​xj​)−(β0​+j=1∑p​E(βj​Xj​))=f^​(x)−E(f(X))

In other words, the total contribution of input xxx is equal to the prediction minus the average prediction.

Since we often work with non-linear models (e.g., ensemble methods), we require a more general approach. We adopt the Shapley value from cooperative game theory to attribute the prediction of any machine learning model to individual feature contributions.

The Shapley value of feature jjj for an instance is defined as:

ϕj(val)=∑S⊆{x1,…,xp}∖{xj}∣S∣!(p−∣S∣−1)!p![val(S∪{xj})−val(S)]\phi_j(val) = \sum_{S \subseteq \{x_1, \ldots, x_p\} \setminus \{x_j\}} \frac{|S|!(p - |S| - 1)!}{p!} \left[ val(S \cup \{x_j\}) - val(S) \right]ϕj​(val)=S⊆{x1​,…,xp​}∖{xj​}∑​p!∣S∣!(p−∣S∣−1)!​[val(S∪{xj​})−val(S)]

Here:

  • SSS is a subset of the full set of features (a coalition),

  • xxx is the feature vector of the instance being explained,

  • pppis the total number of features,

  • ∣S∣!(p−∣S∣−1)!p!\frac{|S|!(p - |S| - 1)!}{p!}p!∣S∣!(p−∣S∣−1)!​ is the Shapley weight for subset ( S ),

  • val(S)val(S)val(S) is the value function (i.e., model output) for feature subset SSS, marginalized over the features not in SSS. The value function valx(S)val_x(S)valx​(S) is computed as:

valx(S)=∫f(x1,…,xp) dPx∉S−EX[f^(X)]val_x(S) = \int f(x_1, \ldots, x_p) \, dP_{x_{\notin S}} - \mathbb{E}_X[\hat{f}(X)]valx​(S)=∫f(x1​,…,xp​)dPx∈/S​​−EX​[f^​(X)]
  • This expression marginalizes over the features not included in SSS. For example, suppose the model uses 4 features x1,x2,x3,x4x_1, x_2, x_3, x_4x1​,x2​,x3​,x4​, and we consider the coalition S=x1,x3S = {x_1, x_3}S=x1​,x3​. Then the value function is:

valx(S)=∫R∫Rf(x1,X2,x3,X4) dPX2,X4−EX[f^(X)]val_x(S) = \int_{\mathbb{R}} \int_{\mathbb{R}} f(x_1, X_2, x_3, X_4) \, dP_{X_2, X_4} - \mathbb{E}_X[\hat{f}(X)]valx​(S)=∫R​∫R​f(x1​,X2​,x3​,X4​)dPX2​,X4​​−EX​[f^​(X)]

SHAP specifies the explanation as:

g(z′)=ϕ0+∑j=1Mϕjzj′g(\mathbf{z}') = \phi_0 + \sum_{j=1}^{M} \phi_j z'_jg(z′)=ϕ0​+j=1∑M​ϕj​zj′​

where:

  • ggg is the explanation model,

  • z′=(z1′,…,zM′)T∈{0,1}M\mathbf{z}' = (z'_1, \ldots, z'_M)^T \in \{0,1\}^Mz′=(z1′​,…,zM′​)T∈{0,1}Mis the coalition vector,

  • MMM is the maximum coalition size, and ϕj∈R\phi_j \in \mathbb{R}ϕj​∈R is the feature attribution for feature jjj, the Shapley values.

What is baseline?

在 TreeSHAP 中,baseline(有时也叫 expected value)是:

模型在训练集或测试集上所有预测值的平均值,即 baseline=E[f(x)]baseline=E[f(x)]baseline=E[f(x)]

  • 对于回归问题:就是所有样本预测值的平均值。

  • 对于二分类(log-odds 模型):是 logit 输出的平均值。

  • 对于概率输出(经过 sigmoid)模型:可以取平均概率作为 baseline(但 TreeSHAP 默认是在 log-odds 上计算)。

What is the SHAP additive?

SHAP 是加性的,但不代表模型是线性的

SHAP 的核心数学基础是 Shapley value,满足以下属性:

对于任意模型 f(x)f(x)f(x),我们可以写成:

f(x)=ϕ0+∑i=1Mϕi f(x) = \phi_0 + \sum_{i = 1}^M \phi_if(x)=ϕ0​+∑i=1M​ϕi​

其中:

  • ϕ0\phi_0ϕ0​​ 是 baseline(即 E[f(x)]\mathbb{E}[f(x)]E[f(x)])

  • ϕi\phi_iϕi​​ 是第 iii 个特征对模型输出的边际贡献(SHAP 值)

SHAP value VS linear coefficient

  • 在线性模型中,系数 β\betaβ 是模型本身学出来的参数。

    • 它反映的是每个特征单位变化对输出的直接影响。

    • 和特征标准化、缩放、共线性密切相关。

  • 在 SHAP 中,ϕ\phiϕ 是模型预测后计算出来的归因值。

    • 它不是模型的参数,而是解释模型预测的“分摊结果”。

    • 会随着特征值、样本、模型结构、甚至特征顺序而变化。

假设你有一个模型预测:

💡 线性模型:

premium=β0+0.8⋅age+2.5⋅claim_count\text{premium} = \beta_0 + 0.8 \cdot \text{age} + 2.5 \cdot \text{claim\_count}premium=β0​+0.8⋅age+2.5⋅claim_count

  • 不论样本是年轻还是年老,age 的单位贡献永远是每多一岁 → 保费涨 0.8 元。

  • 模型结构是“全局一致的”。

💡 非线性模型(如 XGBoost)+ SHAP:

  • 对年轻人,age 的 SHAP 值可能是 -1.0 → 降低保费;

  • 对年长的人,age 的 SHAP 值可能是 +3.0 → 提高保费;

  • 说明模型对 age 的使用是非线性、有阈值的(比如 60 岁以上风险变大)。

Extra

  • mean SHAP value 不等同于线性模型中的系数或贡献,它是对特征在整个样本空间中边际贡献的平均估计。虽然 SHAP decomposition 在形式上是加性的,但它能捕捉模型中复杂的非线性和特征交互,从而为任意模型提供一致的解释。

  • The mean SHAP value reflects the feature’s average marginal contribution to the model output across all data points. A decrease in this value suggests that the model is now less reliant on this feature and more dependent on other variables to explain the predictions. However, this does not imply a consistent 15% contribution as would be the case in a linear model with fixed coefficients. For example, the SHAP value of a feature could account for 10% of the prediction in one data point, and 25% in another — highlighting the local, context-specific nature of SHAP-based explanations.

Reference

PreviousxAINextExtra Research

Last updated 2 months ago

https://christophm.github.io/interpretable-ml-book/shap.html
https://zhuanlan.zhihu.com/p/85791430