TreeIG
TreeIG is a Python package for Integrated Gradients feature attribution on
supported numeric tree models. Install and import it as treeig. Given a fitted
model, a baseline point or weighted background, and evaluation rows, TreeIG
returns feature contributions and completeness diagnostics.
TreeIG computes exact Integrated Gradients for supported numeric tree models. A tree's gradient is zero almost everywhere; its integrated gradient is not.
Check supported models before choosing an interface.
TreeIG uses exact structural split crossings; TreeIGNumeric
is a separately selected numerical fallback. Exact classification explains raw
margins or logits. Exact parsing requires finite numeric inputs and does not
support categorical splits or missing-value routing. Installing the CatBoost
extra does not add an exact CatBoost backend.
Tree ensembles are piecewise constant, so $\nabla F = 0$ except on a measure-zero set of split boundaries. Numerical Integrated Gradients therefore recovers approximately nothing, which is why IG has largely been confined to differentiable models.
The pointwise gradient is not the full derivative. In the distributional sense, $F'$ carries an impulse at each split boundary whose integral equals the prediction jump there.
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 enumerates the boundaries crossed by the straight-line path from baseline to observation, assigns each jump to its split feature, and sums across trees. No quadrature and no sampling are involved, and completeness
$$\sum_j \phi_j = F(x) - F(x_0)$$
holds to the floating-point precision of the fitted model's own arithmetic. Weighted baseline distributions are supported directly.
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).
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
This snippet assumes a fitted supported model and numeric evaluation data. For a standalone example that creates data, fits a model, and checks prediction reconstruction, start with the complete quickstart.
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. For probability-only classifiers, it defaults to binary log odds
or centered multiclass log scores; class probabilities require explicit
probability_to_score=False. Zero probabilities require an explicit
probability_floor for score conversion. A small completeness
residual alone does not establish accurate individual feature allocations. 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
For automated readers, llms.txt maps the guides, complete examples, and rendered API reference.
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.
Related projects
| Package | When to use it |
|---|---|
UnifiedIG (unifiedig) |
A common Integrated Gradients interface across supported tree and smooth model families. |
CBaseline (cbaseline) |
Construct empirical reference distributions; TreeIG accepts its backgrounds with their weights directly. |
skgrad (skgrad) |
Obtain analytic input gradients and Jacobians for supported smooth scikit-learn models. |
Use TreeIG directly when you need its tree-specific attribution interface. See the Integrated Gradients stack for how the packages compose and why output scales must agree.
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.
Release maintainers: see Publishing releases.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file treeig-0.2.2.tar.gz.
File metadata
- Download URL: treeig-0.2.2.tar.gz
- Upload date:
- Size: 113.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
097b4d86dd1d67d7c7ea5be66f8e03f83534b6995849d3755e793b3bc7a48718
|
|
| MD5 |
8d1b460640500bbdcb8f0796896f5f6b
|
|
| BLAKE2b-256 |
35ce688a22ff9e5f249e62b2b7111d4a1875705f8af58ea37b2f99a03efab070
|
Provenance
The following attestation bundles were made for treeig-0.2.2.tar.gz:
Publisher:
release.yml on LudgerHentschel/treeig
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
treeig-0.2.2.tar.gz -
Subject digest:
097b4d86dd1d67d7c7ea5be66f8e03f83534b6995849d3755e793b3bc7a48718 - Sigstore transparency entry: 2811312495
- Sigstore integration time:
-
Permalink:
LudgerHentschel/treeig@00f50b2e59ed16ace1b490f58b38be907047fa62 -
Branch / Tag:
refs/tags/v0.2.2 - Owner: https://github.com/LudgerHentschel
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@00f50b2e59ed16ace1b490f58b38be907047fa62 -
Trigger Event:
push
-
Statement type:
File details
Details for the file treeig-0.2.2-py3-none-any.whl.
File metadata
- Download URL: treeig-0.2.2-py3-none-any.whl
- Upload date:
- Size: 50.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
adabc075c9c063936a55bb6b308ea4f2e4f8e4f21bc3a23583691c51c273bda0
|
|
| MD5 |
25b2dd914ba7fe2dfed610a9bc355c03
|
|
| BLAKE2b-256 |
fe66deb50b952b381b2894e223f23b94384127ede867ba5477618b030a95d219
|
Provenance
The following attestation bundles were made for treeig-0.2.2-py3-none-any.whl:
Publisher:
release.yml on LudgerHentschel/treeig
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
treeig-0.2.2-py3-none-any.whl -
Subject digest:
adabc075c9c063936a55bb6b308ea4f2e4f8e4f21bc3a23583691c51c273bda0 - Sigstore transparency entry: 2811312561
- Sigstore integration time:
-
Permalink:
LudgerHentschel/treeig@00f50b2e59ed16ace1b490f58b38be907047fa62 -
Branch / Tag:
refs/tags/v0.2.2 - Owner: https://github.com/LudgerHentschel
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@00f50b2e59ed16ace1b490f58b38be907047fa62 -
Trigger Event:
push
-
Statement type: