gapnet
An interpretable neural network that tells you whether something affects one group more than another, and by how much.
A normal neural network can predict who will fail an exam, but it can't tell you why, let alone
whether the reasons are different in different places. gapnet is built to answer exactly that:
"Low income hurts pass rates everywhere, but 1.6x more in Mountain districts. Distance to school matters in the Hills and hardly at all in the Valley. Parents' education matters the same everywhere."
It is as accurate as a black-box network, every effect can be read and plotted directly (no SHAP guesswork), and every gap comes with a confidence interval and a significance test. It is built on Keras 3, so the same code runs on TensorFlow, PyTorch or JAX.
pip install "gapnet[jax,plot]" # or gapnet[tensorflow] / gapnet[torch]
Quick start
from gapnet import GapNet, make_example
df = make_example() # simulated school data with known gaps
model = GapNet(group="region").fit(df, target="passed")
print(model.summary())
Real output:
GapNet: 4 features x 4 groups of 'region' (binary, 10-model ensemble, effects in log-odds)
[GAP] baseline: otherwise identical people in 'Mountain' score lower (gap -0.85, 95% CI -1.08 to -0.62)
[GAP] distance_km: effect is 2.2x stronger in 'Hill' (-0.72 vs -0.33 overall; gap -0.39, 95% CI -0.57 to -0.21)
[GAP] income: effect is 1.6x stronger in 'Mountain' (+1.28 vs +0.78 overall; gap +0.49, 95% CI +0.24 to +0.75)
[GAP] distance_km: effect is weak or absent in 'Valley' (+0.16 vs -0.33 overall; gap +0.49, 95% CI +0.24 to +0.75)
[GAP] baseline: otherwise identical people in 'Hill' score higher (gap +0.17, 95% CI +0.08 to +0.27)
[SAME] no evidence of a gap for: parent_edu, female
The data was simulated so that the truth is known: income matters twice as much in Mountain,
distance matters only in Mountain and Hill, parents' education is the same everywhere and gender has
no effect. gapnet found these gaps from the data alone and reported no false ones.
model.plot("income") # one curve per group, with 90% bands
model.gaps() # table: effect, overall, gap, 95% CI, Holm-adjusted p-value
model.effects("distance_km") # the curves as a DataFrame
model.predict_proba(new_df) # it is still a normal predictive model
How it works
For a person in group g:
prediction = bias + b_g + Σ_j [ f_j(x_j) + δ_j(x_j, g) ]
- f_j: a small neural network per feature, the shared effect for everyone.
- δ_j: a small gap network per feature that takes the feature and a learned embedding of the group, and learns how that group's effect curve differs.
- Shrinkage: a penalty pulls gaps towards zero unless the data clearly supports them, much as multilevel (random-effects) models do, so small groups don't produce imaginary gaps.
- Uncertainty: a bootstrap ensemble (10 models by default, each with early stopping on its out-of-bag rows) gives every effect and gap a 95% interval.
- Testing: each gap gets a Student-t test across the ensemble, with Holm correction over all features and groups. A reversal is only reported when the group's own effect passes the same bar.
Because the model is additive, each feature's effect for each group is an exact curve you can plot, not an after-the-fact approximation.
Does it actually work?
It finds the gaps that are there, and only those. On the simulated data above (6,000 rows,
4 regions), at two different random seeds, it found the planted income gap in Mountain and the distance
gaps between Hill, Terai and Valley, and every gap it flagged was a real one. Smaller planted gaps
(distance in Mountain, where data at typical distances is sparse) were not always significant: with
shrinkage, gapnet errs on the side of saying "not enough evidence".
It doesn't invent gaps that aren't there. On data with no group differences (make_example(gaps=False))
it reported 0 false gaps across 5 datasets (4 x 6,000 rows, 1 x 1,500 rows). This is checked in
the test suite.
It's as accurate as a black box. Test-set AUC on the example data (70/30 split):
| Model | AUC | Explains group gaps? |
|---|---|---|
| Logistic regression | 0.745 | No |
| MLP, 2 x 64 (scikit-learn, black box) | 0.755 | No |
| GapNet | 0.753 | Yes, with CIs and tests |
What gaps() reports
| column | meaning |
|---|---|
effect |
change in the outcome when the feature moves from its 25th to 75th percentile (numeric) or from the reference level to this level (categorical), in this group. Log-odds for binary targets, outcome units for regression |
overall |
the size-weighted average effect across groups |
gap, gap_lower, gap_upper |
effect - overall with a 95% interval |
p, p_adj, significant |
Student-t test across the ensemble, Holm-adjusted |
ratio |
effect / overall when they share a sign ("2x stronger") |
(baseline) rows |
otherwise identical people (all features at their median / reference level) in different groups |
Works on any data with groups
Nothing is specific to schools or Nepal. A few examples:
- Health: does smoking raise risk more for women than for men?
- Business: does a price change hit sales harder in one region?
- HR and fairness: does experience affect salary differently by department or gender?
- Agriculture: does rainfall matter more for some districts or crops?
- Policy: where should a programme go, given where a factor matters most?
Numeric, boolean and categorical features are handled automatically (categoricals are one-hot encoded against their first level; missing numeric values are filled with the median and you are warned). Targets can be binary (any two values) or numeric.
Choosing a backend
gapnet uses whichever backend Keras is set to. Set it before importing:
import os
os.environ["KERAS_BACKEND"] = "jax" # or "tensorflow" / "torch"
Options
GapNet(group, task="auto", n_models=10, hidden=(32, 32), gap_hidden=16, embed_dim=4,
gap_penalty=0.03, epochs=300, batch_size=256, learning_rate=3e-3, patience=20,
random_state=0, verbose=0)
gap_penalty: higher means a gap needs more evidence before it shows up.n_models: more models give tighter, more reliable intervals; below 5 the tests have little power.
Limitations (v0.1)
- One grouping column at a time (intersections like region x gender: combine them into one column).
- Effects are additive across features; interactions between features are not modelled yet.
- Curves are least certain at the extremes of a feature's range, where there is little data. Read the bands, not just the lines.
- Binary and regression targets only (no multi-class yet).
Related work
gapnet builds on Neural Additive Models (Agarwal et al., 2021), varying-coefficient models
(Hastie & Tibshirani, 1993) and multilevel models. Its contribution is a single neural model that learns
group-specific effect curves with shrinkage and reports the gaps with uncertainty and multiple-testing
control.
Also by the author
- simpsonscan: find Simpson's paradoxes in your data
- chartaudit: catch misleading charts (lie factor, data-ink, Anscombe)
- pandas-mergeguard: predict and audit pandas merges before they break
Citation
If you use gapnet in research, please cite:
Adhikari, A. (2026). gapnet: an interpretable neural network for group-specific effects (v0.1.1).
https://github.com/AnmolAdhikari/gapnet
MIT licensed. Contributions welcome.
Metadata
Release files for gapnet 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| gapnet-0.1.1.tar.gz | 40.8 kB | With provenance | Details for gapnet-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| gapnet-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for gapnet-0.1.1-py3-none-any.whl |
Total release size: 59.4 kB
Release files / gapnet-0.1.1.tar.gz
| Download URL | gapnet-0.1.1.tar.gz |
|---|---|
| Size | 40.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
903d064ea0bab09c4fa715efa5e514e70d1daadda33d11dfb8a19b0737d69e8c
|
|
BLAKE2b-256 checksum How to use checksums |
4dd03e25bd4b381b31066ae756a8e023620e33afc06b718ac7e1d8d19c7b37ad
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.
Transparency logRelease files / gapnet-0.1.1-py3-none-any.whl
| Download URL | gapnet-0.1.1-py3-none-any.whl |
|---|---|
| Size | 18.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e5948274c20e880b19a2f8819b4d773af925fb5ed51d890b5e919c0b478b7676
|
|
BLAKE2b-256 checksum How to use checksums |
67137c29844f0f02d51b41b3e3d5f9e1b38575ee2e813930800e26b97fb3a785
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.
Transparency logSecurity for gapnet 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for gapnet-0.1.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.