Exact Shapley, anchors, diverse counterfactuals, influence and the audit
Exact Shapley values for small models, rules that hold a prediction in place, several ways to change it, the training rows behind it, and a check of whether an explanation is faithful to the model at all.
What runs in CodeSplash
- Linear / logistic regression Verified in Python and R
- XGBoost Verified in Python and R
- Random forest Verified in Python and R
You will be able to
- Know when exact Shapley values replace KernelSHAP
- Read an anchor's precision, its lower bound and its coverage
- Use diverse counterfactuals and influence to answer "what would change it" and "what taught it this"
- Read the deletion check and explainer agreement before trusting an explanation
Four more questions
ReadThe Explain tab in Predict adds these to the explainers in the previous lesson. Each answers its own question.
| Question | Explainer | Check its answer with |
|---|---|---|
| Exactly how much did each feature add? | Exact ShapleyExact Shapley valuesShapley values computed from every combination of features, so they add up exactly to each prediction minus the average. | The values add up to the score minus the average |
| What rule keeps this prediction? | AnchorsAnchorA short rule built from a row's own values that keeps its prediction for most rows that satisfy it. | Precision, its lower 95% bound, Coverage |
| What different changes would flip it? | Diverse counterfactualsDiverse counterfactualsSeveral different changes that would each move a prediction across its target, chosen to be close to the row and far apart. | Each option's Score after and Distance |
| Which training rows taught it this? | Training rows behind itInfluence (training rows)How much a prediction would move if one training row were left out and the model refitted. | Effect and Leverage |
Exact Shapley values
ReadKernelSHAP estimates Shapley values from a sample of feature combinations. With few features you don't need to estimate: Exact Shapley evaluates all 2^p combinations against the same background rows. With 8 features that is 256 combinations per row. With 12 it is 4,096. Above 12, CodeSplash says so and points you back to KernelSHAP.
Use the exact values as a reference. If KernelSHAP's mean |φ| differs from the exact values by more than a few percent, raise its coalitions or background rows. The audit below flags that gap.
Anchors
ReadAn anchor is a short rule built from the row's own values, such as glucose > 140 AND age > 29, that keeps the prediction for most training rows satisfying it.
- Precision is the share of training rows satisfying the rule that get the same prediction.
- Precision, lower 95% is the Wilson lower bound. A rule supported by 12 rows can show 100% precision with a lower bound near 75%.
- Coverage is the share of all training rows the rule applies to. Short rules cover more rows.
The search adds the condition that raises precision most until it reaches the target (95% by default).
Diverse counterfactuals
ReadThe greedy counterfactual gives one smallest change. Diverse counterfactuals give several different ones, in the style of DiCE. CodeSplash draws random candidate changes and keeps those that reach the target. It picks the closest first, then the ones farthest from those already picked, and drops any change an option doesn't need. Options let a person choose a change they can actually make. Mark what they can't change under Never change.
Training rows behind a prediction
ReadFor linear and logistic models, the explainer shows which training rows moved each prediction most. Effect is how much the prediction would drop if that row were left out and the model refitted. It is exact for least squares and a one-step approximation for logistic regression. Rows with high Leverage (unusual feature values) and large residuals matter most. A single row with a huge effect is worth opening: it may be a data error.
Is the explanation faithful?
ReadCheck the explanations runs a deletion check. For a few rows it removes features in the order the attributions rank them, and again in a random order. A removed feature takes the background rows' values.
View data
| share of features removed | share of the score left | order |
|---|---|---|
| 0 | 1 | By TreeSHAP |
| 0.1250 | 0.5920 | By TreeSHAP |
| 0.2500 | 0.2950 | By TreeSHAP |
| 0.3750 | 0.2060 | By TreeSHAP |
| 0.5000 | 0.1460 | By TreeSHAP |
| 0.6250 | 0.0740 | By TreeSHAP |
| 0.7500 | 0.0570 | By TreeSHAP |
| 0.8750 | 0.0330 | By TreeSHAP |
| 1 | 0 | By TreeSHAP |
| 0 | 1 | Random |
| 0.1250 | 0.8300 | Random |
| 0.2500 | 0.6840 | Random |
| 0.3750 | 0.6350 | Random |
| 0.5000 | 0.5620 | Random |
| 0.6250 | 0.4550 | Random |
| 0.7500 | 0.2070 | Random |
| 0.8750 | 0.1050 | Random |
| 1 | 0 | Random |
If the ranking is faithful, the score falls faster when features go in ranked order. Faithfulness is the random curve's area minus the attribution curve's area. Positive is good; near zero means the ranking tells you little about this model.
Do the explainers agree?
ReadThe audit also compares the explainers you ran. Spearman's ρ between their feature rankings and the overlap of their top three show how alike they are. Permutation importance and SHAP answer different questions: shuffling a feature versus its share of each prediction. Low agreement is a reason to look at correlated features, not proof that one of them is wrong.
Check yourself
A model has 9 features. KernelSHAP's mean |φ| for income is 0.31 and exact Shapley's is 0.22. What should you do?
Show the answer
Answer: b
With 9 features exact enumeration is cheap (512 combinations). KernelSHAP is an estimate of the same values, so a large gap means its sample was too small.
Check yourself
An anchor shows precision 100%, lower 95% 0.76 and coverage 0.02. How should you read it?
Show the answer
Answer: b
Coverage 0.02 means the rule applies to 2% of training rows. The Wilson lower bound shows how little those rows can prove.
Check yourself
Why ask for several counterfactuals instead of the single smallest change?
Show the answer
Answer: a
Counterfactuals describe the model's decision boundary. Diversity is about choice, not causation.
Check yourself
One training row has an effect of +0.18 on a prediction of 0.62 from a logistic model. What does that mean?
Show the answer
Answer: a
Influence traces a prediction back to the rows that taught it. For logistic regression it is a one-step approximation of the refit.
Check yourself
The deletion check reports faithfulness 0.01 for KernelSHAP on a model with 6 features. What does it tell you?
Show the answer
Answer: b
Faithfulness compares the attribution's deletion curve with a random order's. A gap near zero means the ranking isn't tracking what moves the prediction, sometimes because correlated features share the credit.
Concepts in this lesson