shap-recommender
Exclusion / non-linearity / interaction recommendations from saved SHAP attribution files, and application of them to a design matrix.
One shared idea runs through two of the three rules. A feature's own contribution is represented flexibly (indicator columns when it takes few values, a restricted cubic spline when it is continuous), and a model built on that flexible basis is compared against a straight line in the feature. That comparison answers two different questions:
- non-linearity -- does the attribution deviate from a linear function of the feature? This directly tests the linear-trend assumption, rather than relying on a raw correlation coefficient, which conflates "no effect" with "non-linear effect".
- interaction -- does the attribution vary among subjects who share the same feature value? Under additivity the attribution is a deterministic function of the feature, so residual dispersion implies effect modification.
Stratification for the interaction screen is always applied to the observed
feature value at a pre-specified cut point, never to the attribution itself
-- splitting on the attribution would condition on the candidate modifier.
Within strata, the contrast E[phi_y | y=1] - E[phi_y | y=0] is compared,
which (unlike the marginal attribution distribution) is invariant to
stratum composition under additivity.
Install
pip install shap-recommender
Expected input files
For each dataset "tag" you want to load, Recommender.load(tag) (and the
CLI's --tags) expects two tab-separated files in res_dir:
shap_values_<tag>.tsv-- SHAP values, one row per subject, one column per feature, first column = row index.sel_data_<tag>.tsv-- the corresponding feature values (design matrix), same row index.
Command-line use
shap-recommender \
--res-dir ./shap_results \
--tags cohort_a cohort_b \
--nonlinear-candidates age bmi creatinine \
--out ./recommendations
This writes exclusion_tests.tsv, exclusion_sensitivity.tsv,
nonlinear_tests.tsv, interaction_tests.tsv, attribution_patterns.tsv,
and recommendations.json to --out. Run shap-recommender --help for all
options (thresholds, bootstrap count, spline degrees of freedom, a
--cutpoints JSON file for pre-specified stratification cut points, etc).
Library use
from shap_recommender import Recommender
rec = Recommender(res_dir="./shap_results")
recommendations = rec.generate(
candidates_nonlinear=["age", "bmi", "creatinine"],
tags=["cohort_a", "cohort_b"],
out="./recommendations",
)
# apply the recommendations to a design matrix
X_train_adj, X_test_adj = Recommender.apply(
X_train, X_test, recommendations, variant="all",
)
Recommender.apply(..., variant=...) accepts "baseline", "exclusion",
"nonlinear", "interaction", or "all", so each rule's effect on
downstream model performance can be evaluated separately.
Validating the interaction rule
Recommender.null_sim() runs a small simulation under an additive null
(no true interaction with the feature being tested) and reports the type-I
error rate of the within-stratum contrast used by stratified_screen,
compared against naively partitioning on the attribution itself:
from shap_recommender import Recommender
Recommender(res_dir=".").null_sim()
License
MIT
Release files for shap-recommender 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| shap_recommender-0.1.0.tar.gz | 14.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| shap_recommender-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.3 kB
Release files / shap_recommender-0.1.0.tar.gz
| Download URL | shap_recommender-0.1.0.tar.gz |
|---|---|
| Size | 14.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
da4feaa20f73f14c6907f66df02483bab95fe59ce984158b3881a781ea7bc5d2
|
|
BLAKE2b-256 checksum How to use checksums |
e063d6a13da1703b270583327cd0bff2ddb2d3a588e88523ffdd234138ad209d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.5
|
Release files / shap_recommender-0.1.0-py3-none-any.whl
| Download URL | shap_recommender-0.1.0-py3-none-any.whl |
|---|---|
| Size | 13.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
382ee5d00a014bfe684129a1176f1c9442f0d47f9184918d7b6ac86c2e19ed44
|
|
BLAKE2b-256 checksum How to use checksums |
6528abbfb2fb27d2a28e1c9943033d3da11d2e7422662a83e3d21644fe2b4620
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.5
|