Sub topic 1
From Game Theory to Feature Attribution: Shapley Values
Prerequisites
This chapter assumes that you can train and call a scikit-learn-style model, use basic NumPy and pandas, and understand tree ensembles such as gradient boosting.
From game theory to feature attribution: Shapley values
A Shapley value comes from cooperative game theory. A game has players who work together to produce a payoff. In machine learning, the features are the players, and the model output is the payoff.
For one row, SHAP asks: how much does each feature contribute to moving the model output away from a reference output? It considers every possible order in which features could be added. A feature's contribution is its average added value across those orders.
This approach shares interaction effects fairly. For example, if two features only matter together, their interaction contribution is divided between them.
Background data: what it means to remove a feature
To calculate a contribution, SHAP compares a prediction with and without a feature. Removing a feature means replacing its value with values from background data, also called the reference dataset. This dataset represents typical inputs.
The choice matters. A background sample from one population can produce different explanations from a sample from another population. For correlated features, independently replacing one feature can also create unrealistic rows.
The baseline value
The baseline is the model's expected output on the background data. For a regression model, it is usually an average prediction. Each feature's SHAP value measures movement above or below this baseline.
Additivity: prediction = baseline + sum of SHAP values
For a prediction, SHAP provides the local accuracy relationship:
prediction = baseline + SHAP(feature 1) + ... + SHAP(feature n)
The units match the model output used by the explainer. A positive value raises the output above the baseline; a negative value lowers it.
Choosing the right explainer
Choose an explainer that matches the model and output:
TreeExplaineris usually fast and accurate for decision trees, random forests, and gradient-boosted trees.LinearExplainerfits linear models.DeepExplainertargets supported neural networks.shap.Explainercan select a model-agnostic method when no specialized explainer applies. Permutation and Kernel methods can explain arbitrary prediction functions but usually cost more computation.
Exercise
A model predicts 18, its baseline is 12, and two SHAP values are 4 and 2. What is the remaining feature contribution if there are three features and the third contribution is -1?
Show answer
The values sum to 12 + 4 + 2 - 1 = 17, so the stated values do not explain the prediction. The third contribution must be 0 for the prediction to equal 18.