Skip to main content

treecf

DOI PyPI Python CI License

Constrained, threshold-aware counterfactual explanations for tree ensembles.

treecf answers the question: "what is the minimal, feasible change to this instance such that the model's output lands in a target interval?" — for XGBoost, LightGBM, CatBoost and scikit-learn tree ensembles.

Lever-set by feature matrix of a recourse menu: filled cells where a plan changes that lever, a square for a proved-optimal plan, a cross for a lever set certified unable to reach the target

On PyPI. See the documentation for concepts and tutorials.

Why another counterfactual package?

  • Tree-native and fast. Models are parsed into a shared tree IR and the constrained search runs on a bundled Rust core, typically in milliseconds; every result is float-verified against the parsed model before it is returned, and the parsers are conformance-tested against the native library.
  • Optional proofs, inside a measured envelope. backend="exact" returns proof="optimal" when no cheaper plan exists under the declared objective (weighted distance, plus a per-feature term only if you set sparsity_weight), and a completed search that finds nothing returns Infeasible(proof="certified"). Proofs scale with the number of levers the search may move, not with the model's width: on the measured matrix the refine search certifies up to 200 trees with 12 free features inside 60 s and no 20-feature model beyond the smallest one (50 trees at depth 3) — while on the 300-tree, 50-feature model a coalition of up to three levers certifies in under half a second with search="refine", so wide models get proofs once the levers are restricted with Freeze, coalitions, or a recourse_menu. A search that runs out of budget returns its best plan labelled heuristic and warns; it never claims more.
  • Recourse regions. Any verified counterfactual widens into a certified box — "reduce utilization below 0.40", not "to 0.3972" — with every point in the box provably in-target and constraint-feasible; works with every backend.
  • Real constraints. Immutability, directionality, ranges, one-hot consistency, linear inter-feature rules such as max_dpd_30d <= max_dpd_12m, and NaN as a legitimate value with its own transition cost — declared once, enforced by every backend.
  • Menus, diverse plans, certificates. recourse_menu solves every lever set up to a size and says which combinations provably cannot work; explain_diverse returns the cheapest plans with distinct lever sets; certificate turns any result into a self-contained JSON record a validator re-checks later.

On a 120-tree model and 100 declined rows, treecf's plans cost a seventh of DiCE's at a fifth of the time; NICE is four times faster per instance, and its plans cost 2.7 times more and cannot take constraints. The measured tables and the honest reading are on the benchmarks page.

Not for you if: the model is not a tree ensemble; you want sets of plans diverse by distance rather than by the levers they use (DiCE does that); you need a proof over dozens of free levers at once without restricting them (see the proof envelope); or you need a frozen API — treecf is in beta, see API stability.

Installation

pip install "treecf[xgboost,viz]"   # wheels for Linux, macOS, Windows; no Rust toolchain needed

numpy is the only Python dependency; the extras add a parser for your training library and the plots. JSON model dumps parse without the training library, so explanations can be generated on a scoring host that has neither it nor a solver.

Quick look

credit_demo() returns a packaged credit model, background rows, and one declined applicant. It is new in this version; on an older installed package, substitute your own model, as the quickstart notebook does.

from treecf import Explainer, Freeze, Monotone, Target
from treecf.datasets import credit_demo

model, X, x = credit_demo()
target = Target.probability(range=(0.0, 0.05))
exp = Explainer(
    model, background=X,
    constraints=[Freeze("occupation"), Monotone("tenure_months", "increase")],
)

res = exp.explain(x, target=target, seed=0)
res.changes                          # {'income': (4678.0, 6932.4)}
res.proof                            # 'heuristic'

proved = exp.explain(x, target=target, backend="exact", region=True, seed=0)
proved.proof                         # 'optimal'
proved.region.describe()             # {'income': 'in [6.58e+03, 7.81e+03] (data-limited)',
                                     #  'utilization': 'in [0.428, 0.541)', ...}

menu = exp.recourse_menu(x, target=target, max_levers=2)
menu.describe()["dpd_12m"]           # 'no acceptance is reachable by changing only dpd_12m'

Learn more

Cite

@software{wlazlo_treecf,
  author  = {Wlazło, Daniel},
  title   = {treecf: constrained, threshold-aware counterfactual explanations for tree ensembles},
  doi     = {10.5281/zenodo.22069503},
  url     = {https://github.com/wlazlod/treecf},
  license = {MIT}
}

Contributing and license

MIT. See CONTRIBUTING.md for dev setup, the test layers, and the project's hard invariants; report security issues privately per SECURITY.md.

Metadata

Release files for treecf 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for treecf 0.3.2
File Size Uploaded
treecf-0.3.2.tar.gz 320.4 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for treecf 0.3.2
File
treecf-0.3.2-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details
treecf-0.3.2-cp311-abi3-musllinux_1_2_x86_64.whl CPython 3.11 abi3 Linux musl 1.2+ x86-64 Details
treecf-0.3.2-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 abi3 Linux glibc 2.17+ x86-64 Details
treecf-0.3.2-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.11 abi3 Linux glibc 2.17+ ARM64 Details
treecf-0.3.2-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details
treecf-0.3.2-cp311-abi3-macosx_10_12_x86_64.whl CPython 3.11 abi3 macOS 10.12+ x86-64 Details

Total release size: 5.1 MB

Release files / treecf-0.3.2.tar.gz

Download URL treecf-0.3.2.tar.gz
Size 320.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c64cbfe984847c5d530cd82621e774f8345983eb09dc8f9844ee90e15acae079
BLAKE2b-256 checksum
How to use checksums
088730b6414359232f17698ed89025640744d5ca03e3b0b8c3af52c9eb32ea32
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 6, 2026.

Transparency log

Release files / treecf-0.3.2-cp311-abi3-win_amd64.whl

Download URL treecf-0.3.2-cp311-abi3-win_amd64.whl
Size 694.3 kB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
f94e638f1b41fb0cc9239f7c30833d02b9ac492e84a7041515dc67419a7853fd
BLAKE2b-256 checksum
How to use checksums
be1b6c4526229d6078443c956c891719e8dfe252e5ec97dbdd977eb7b4857bf1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 6, 2026.

Transparency log

Release files / treecf-0.3.2-cp311-abi3-musllinux_1_2_x86_64.whl

Download URL treecf-0.3.2-cp311-abi3-musllinux_1_2_x86_64.whl
Size 1.0 MB
Tags CPython 3.11 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
e43e1a2cb3513b608d22a693d4bab70dab2e7950236a78e1043f8ba6fc777862
BLAKE2b-256 checksum
How to use checksums
5942f0f475b1c72a9efa6327eef32eec267acd1353ddbeeff855be0bf54362d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 6, 2026.

Transparency log

Release files / treecf-0.3.2-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL treecf-0.3.2-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 798.1 kB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
996157b5ed286199f49ee60e5eff6339650a6c681c1546a9d7fa26da94969db6
BLAKE2b-256 checksum
How to use checksums
35c738fab15941113e61ff5946ad05c81075243a1be00a9c799081fc02ea3160
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 6, 2026.

Transparency log

Release files / treecf-0.3.2-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL treecf-0.3.2-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 771.8 kB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
ed1c6674611b8447b3b235b2f8df322d5b4cccdff0d7505b8e8a304d4a19fcb7
BLAKE2b-256 checksum
How to use checksums
8acb8aed6d6c3ab6b131f3477dd74043c3310c208cf4a2baf12f5c16a289e8c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 6, 2026.

Transparency log

Release files / treecf-0.3.2-cp311-abi3-macosx_11_0_arm64.whl

Download URL treecf-0.3.2-cp311-abi3-macosx_11_0_arm64.whl
Size 733.8 kB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
40dc52fe49fb8a11050e26a1611e9e758997dbaab6fd5ab29ded633483694055
BLAKE2b-256 checksum
How to use checksums
a5ecb776ecc7bdcf8b3fab01c463a77c5a40c10f449d1850667df7b891e6fa9b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 6, 2026.

Transparency log

Release files / treecf-0.3.2-cp311-abi3-macosx_10_12_x86_64.whl

Download URL treecf-0.3.2-cp311-abi3-macosx_10_12_x86_64.whl
Size 758.1 kB
Tags CPython 3.11 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
01af5d830c709b7d96ebac77852c07d59f0ea97b7fe2ab9f68ae6528108ac547
BLAKE2b-256 checksum
How to use checksums
9cf8c3abcfc19130ce458a5290a79b7f02abeb2505ea92e9722e92f9e651fcf7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 6, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.3.2 This release

7 release files

0.3.1

7 release files

0.3.0

7 release files

0.2.4

7 release files

0.2.3

7 release files

0.2.2

7 release files

0.2.1

7 release files

0.2.0

7 release files

0.1.1

7 release files

0.1.0

7 release files

0.0.1

7 release 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