Skip to main content

TreeIG

PyPI version Documentation

TreeIG computes exact Integrated Gradients for tree-based models. It attributes the change in a model's scalar output from a baseline to an observation to the input features. For one baseline $x_0$,

$$\sum_j \phi_j = F(x) - F(x_0).$$

TreeIG finds the split boundaries crossed along the straight-line path and sums their prediction jumps. For supported models, this avoids numerical integration and sampling. Weighted baseline distributions are supported as well.

The method is developed in Ludger Hentschel's TreeIG: Exact Integrated Gradients for Tree-Based Models. It builds on Integrated Gradients introduced by Sundararajan, Taly, and Yan in Axiomatic Attribution for Deep Networks (ICML 2017).

Why Integrated Gradients works for trees

A tree prediction is constant between splits, so its ordinary gradient is zero almost everywhere. But the prediction jumps at split boundaries. Those jumps are the contribution that an ordinary pointwise gradient misses: in the distributional interpretation, each jump is an impulse whose integral equals the jump's height.

A prediction step, its derivative impulse, and its integrated contribution

The top panel shows a single prediction step; the middle shows its derivative as an impulse at the split; the bottom shows the accumulated contribution. Integrating across the split recovers the prediction change. TreeIG applies this idea along the path from a baseline to an observation, assigning each crossing's jump to its split feature and summing across trees.

Installation

pip install "treeig[sklearn]"

Requires Python 3.9 or later, NumPy, and Numba. Install the model library you use; extras include sklearn, xgboost, lightgbm, and catboost. SHAP is optional for plotting. The first attribution call includes Numba compilation.

Quickstart

With a fitted supported model and numeric evaluation data:

from treeig import TreeIG

# A representative training row provides a simple reference.
ig = TreeIG(model, baseline=X_train[0])
result = ig.explain(X_eval)
phi = result.values
print(result.max_abs_completeness_error)

phi has one row per observation and one column per feature. Positive values increase the explained output relative to the baseline; negative values decrease it. Use ig.attribute(X_eval) when only the attribution array is needed.

The baseline defines the comparison. For substantive attribution, CBaseline is the recommended way to construct a prediction-neutral baseline distribution. TreeIG accepts its Background directly as baseline=background, or a matrix of rows with baseline_weights. See the baseline guide.

Model support and interpretation

Exact backends cover selected scikit-learn tree regressors and gradient boosting, XGBoost, and LightGBM. Regression explains predictions; classification explains raw margins, not probabilities. Inputs must be finite and numeric; categorical splits and missing-value routing are not supported by the exact parser.

TreeIGNumeric provides a numerical fallback for other piecewise-constant models, including numeric-input CatBoost and probability-only classifiers. Its resolution requires care. See supported models and the numerical guide.

TreeIG and TreeSHAP answer different attribution questions. TreeIG can be fast on substantial attribution workloads, but relative speed depends on the model, baselines, and batch size. The comparison and benchmarks explain the distinction and report measured examples.

Documentation

The user guide covers a complete runnable example, baseline distributions, classification, plots, loss attribution, numerical conventions, and performance. The Sphinx sources also build into searchable HTML with an API reference; see building the documentation.

Optional GPU support

TreeIG is already fast enough for most applications and remains the default. When attribution speed matters and an NVIDIA GPU is available, GPUTreeIG can be materially faster; recorded T4 comparisons show roughly 9–20× speedups on the reported workloads. Performance depends on the problem. See GPU documentation for installation and limitations.

Citation and license

If you use TreeIG in your work, please cite the TreeIG paper:

@misc{hentschel2026treeig,
  author = {Hentschel, Ludger},
  title  = {{TreeIG}: Exact Integrated Gradients for Tree-Based Models},
  year   = {2026},
  url    = {https://www.ludgerhentschel.com/PDFs/Hentschel%20'26g.pdf},
}

Released under the BSD-3-Clause license.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

treeig-0.2.1.tar.gz (107.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

treeig-0.2.1-py3-none-any.whl (49.8 kB view details)

Uploaded Python 3

File details

Details for the file treeig-0.2.1.tar.gz.

File metadata

  • Download URL: treeig-0.2.1.tar.gz
  • Upload date:
  • Size: 107.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for treeig-0.2.1.tar.gz
Algorithm Hash digest
SHA256 dcffb20bc4ac87c9f468c6e728255641b76e18c122e81cabd6b497402b368fb1
MD5 177d077e8527e32606487a7310bd6d58
BLAKE2b-256 4617d5dd63d6fbafc7a054da124889118065ab08bb4dea11c38dd42ca5b63d4c

See more details on using hashes here.

Provenance

The following attestation bundles were made for treeig-0.2.1.tar.gz:

Publisher: release.yml on LudgerHentschel/treeig

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file treeig-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: treeig-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 49.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for treeig-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3fd35eec258b7cc94c8cd42e9f4d62ee2773d3993052b1c73fd1a8cc30c743e8
MD5 c5d02f0f026398d752fd109a980bbafc
BLAKE2b-256 1e7e01d0d9e7fcb6d8d844e6222b17561018394975bceda8ae5ca5ad9ad4c30b

See more details on using hashes here.

Provenance

The following attestation bundles were made for treeig-0.2.1-py3-none-any.whl:

Publisher: release.yml on LudgerHentschel/treeig

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.2.2

2 files

This release

0.2.1 This release

2 files

0.2.0

2 files

0.1.14

2 files

0.1.11

2 files

0.1.10

2 files

0.1.8

2 files

0.1.7

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page