API reference¶
Explainer and results¶
Counterfactual explainer for a tree-ensemble model.
model may be a native model object, a dump file path/dict, or an
EnsembleIR. background fits the distance normalizers;
alternatively pass normalizers explicitly (array or name->sigma dict).
Source code in src/treecf/api.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 | |
explain(x, target, backend='genetic', time_budget_s=10.0, sparsity_weight=0.0, seed=None)
¶
Search for a counterfactual (or one per band for Target.bands).
backend="genetic" runs the bundled Rust engine (default);
backend="python" runs the reference numpy implementation of the
same algorithm. Every result is float-verified before being returned.
Source code in src/treecf/api.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
explain_batch(X, target, n_per_example=1, diversity='seeds', ids=None, backend='genetic', time_budget_s=10.0, sparsity_weight=0.0, seed=0, coalitions=None, include_full=False)
¶
Mass-produce counterfactuals for a dataset; see treecf.batch.
n_per_example alternatives per row via diversity="seeds" (distinct
change-sets from different seeds, best-effort) or "lever-blocking"
(freeze each plan's biggest lever; also records essential levers).
diversity="coalitions" instead produces one plan per named feature
group in coalitions per row (n_per_example unused; see
explain_coalitions). The returned BatchResult supports
save/load/for_id/to_frame.
Solves run in parallel inside the Rust engine; time_budget_s is
per solve, so a solve that hits its wall-clock budget while sharing
cores may stop earlier than it would sequentially (results are
otherwise identical to solving row by row).
Source code in src/treecf/api.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
explain_coalitions(x, target, coalitions, include_full=False, backend='genetic', time_budget_s=10.0, sparsity_weight=0.0, seed=None)
¶
One counterfactual per named feature coalition (opt-in mode).
Each coalition is solved with every feature outside it frozen, so a
plan only ever asks for changes within one group — grouped recourse
instead of one plan that mixes unrelated levers. Coalitions may
overlap; features in no coalition are never modified; an
Infeasible for a coalition means that group alone cannot reach
the target. include_full=True prepends an unrestricted baseline
under the reserved key "(all levers)". One solve per coalition
(milliseconds each); this mode is optional and never the default.
Source code in src/treecf/api.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
Source code in src/treecf/api.py
68 69 70 71 72 73 74 75 76 77 78 | |
Batch production¶
Counterfactuals for a whole dataset, addressable by row id.
Source code in src/treecf/batch.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
to_frame()
¶
One row per (id, k), wide cf_<feature> columns (pandas, lazy import).
Source code in src/treecf/batch.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
One counterfactual (or the infeasibility marker) for one dataset row.
Source code in src/treecf/batch.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
Targets¶
Closed interval target, expressed in raw-score or probability space.
Target.bands builds a named ladder of intervals (rating grades);
Explainer.explain then returns one result per band.
Source code in src/treecf/targets.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
raw_interval(link)
¶
Interval [L, U] on the raw score; probability targets require the SIGMOID link.
Source code in src/treecf/targets.py
67 68 69 70 71 72 73 74 75 76 | |
Constraints¶
Parse "2*a - b <= 3"-style sugar into a canonical Linear object.
When feature_names is given, identifiers are validated immediately;
otherwise validation happens later in compile_constraints.
Source code in src/treecf/constraints/parser.py
35 36 37 38 39 40 41 42 43 | |
Canonical constraint objects. Frozen dataclasses; validation at compile time.
M1 subset: Freeze, Monotone, Range. Linear, Implies, OneHot, AllowMissing arrive in M2.
AllowMissing
dataclass
¶
NaN is a feasible counterfactual value for this feature.
delta_miss prices the value<->NaN transition; pass delta_from_miss
for an asymmetric NaN->value cost (defaults to delta_miss).
Source code in src/treecf/constraints/objects.py
73 74 75 76 77 78 79 80 81 82 83 | |
Equals
dataclass
¶
Binary-feature equality (used standalone or inside Implies).
Source code in src/treecf/constraints/objects.py
50 51 52 53 54 55 | |
Freeze
dataclass
¶
The feature is immutable: the counterfactual keeps the factual value.
Source code in src/treecf/constraints/objects.py
11 12 13 14 15 | |
Implies
dataclass
¶
If condition holds then consequence must hold; binary features only (v0.1).
Source code in src/treecf/constraints/objects.py
58 59 60 61 62 63 | |
Linear
dataclass
¶
Linear inter-feature constraint: sum(coef * feature) op rhs.
missing_policy resolves the constraint when a referenced feature is NaN
in the counterfactual: "satisfied" (vacuously true, the default),
"violated"/"forbid_missing" (the counterfactual may not use NaN there).
Source code in src/treecf/constraints/objects.py
35 36 37 38 39 40 41 42 43 44 45 46 47 | |
Monotone
dataclass
¶
The feature may only move in one direction from the factual value.
Source code in src/treecf/constraints/objects.py
18 19 20 21 22 23 | |
OneHot
dataclass
¶
The listed binary columns sum to exactly one.
Source code in src/treecf/constraints/objects.py
66 67 68 69 70 | |
Range
dataclass
¶
Hard domain bounds for the counterfactual value (inclusive).
Source code in src/treecf/constraints/objects.py
26 27 28 29 30 31 32 | |
Mining¶
Source code in src/treecf/mining.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
Source code in src/treecf/mining.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
Source code in src/treecf/mining.py
59 60 61 62 63 64 65 66 | |
Plausibility¶
Source code in src/treecf/plausibility.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
Visualization¶
Counterfactual visualizations. matplotlib lives behind the [viz] extra.
plot_alternatives(results, explainer=None, ax=None)
¶
Overlaid dumbbells: every alternative plan's changes for one instance.
Accepts a sequence of Counterfactual objects or feasible
BatchRecord entries, or a mapping of outcomes as returned by
explain_coalitions (keys become legend labels; Infeasible values
are skipped). Each plan keeps one color across all its changes — meant
for a handful of alternatives for the same row (at most 10). With
explainer, changes are plotted as standardized deltas from the
factual (Δ/σ), so features of different scales share one axis; without,
raw values are shown with gray factual dots.
Source code in src/treecf/viz.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
plot_changes(cf, ax=None)
¶
Dumbbell chart of per-feature changes (from -> to); NaN transitions annotated.
Source code in src/treecf/viz.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
plot_counterfactuals(results, ax=None)
¶
Changed-feature matrix comparing diverse counterfactuals.
Source code in src/treecf/viz.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
plot_effort(explainer, cf, ax=None)
¶
Cost-space companion: how the distance J splits across the changes.
Source code in src/treecf/viz.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | |
plot_ladder(bands_result, ax=None)
¶
Cost of reaching each rating band (Target.bands): the price of every grade.
Source code in src/treecf/viz.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
plot_tradeoff(results, target=None, ax=None)
¶
Cost vs achieved score for alternative plans of one instance.
One dot per plan: x = distance J, y = the achieved probability (sigmoid
models) or raw score. target draws the interval bounds the plans had
to reach. Accepts a sequence of Counterfactual objects or feasible
BatchRecord entries, or a mapping as returned by
explain_coalitions (keys label the dots; Infeasible skipped).
Source code in src/treecf/viz.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
plot_waterfall(explainer, cf, target=None, ax=None)
¶
SHAP-style waterfall: exact score deltas of the counterfactual's changes.
Starts at the factual score, applies the changes one at a time (largest single effect first), each bar being the EXACT score delta from that change (recomputed through the IR — endpoints are exact; per-bar attribution is sequential and therefore order-dependent, like any sequential decomposition). Sigmoid-link models are plotted in probability space.
Source code in src/treecf/viz.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
Batch-level counterfactual visualizations. matplotlib lives behind the [viz] extra.
Every function consumes a BatchResult. k=0 (the default) keeps each
row's best plan; k=None keeps every feasible plan, so shares are per plan,
not per row.
plot_batch_deltas(batch, explainer=None, k=0, top_n=10, ax=None)
¶
Strip plot of actual deltas (to − from) per feature, top-N most-changed.
One jittered dot per plan, a median tick per feature; NaN transitions are
counted in a per-feature annotation instead of plotted. With explainer,
deltas are divided by the per-feature normalizer sigma so features of
different scales share one axis.
Source code in src/treecf/viz_batch.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
plot_batch_levers(batch, k=0, normalize=True, top_n=20, show_essential=True, ax=None)
¶
Horizontal stacked bars: share of plans changing each feature, by direction.
Increases, decreases, and NaN transitions stack per feature, ordered by how
often the feature is used. For diversity="lever-blocking" results,
features recorded as essential levers are annotated with their count.
Source code in src/treecf/viz_batch.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
plot_batch_matrix(batch, explainer=None, k=0, sort_rows=True, max_row_labels=30, ax=None)
¶
Plans × features heatmap: binary changes, or effort-shaded with an explainer.
With explainer, each cell shows the change's effort w·|Δ|/σ (NaN
legs priced via AllowMissing); without, cells mark changed features
like plot_counterfactuals. Rows sort by distance; columns by how often
the feature is changed.
Source code in src/treecf/viz_batch.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
plot_batch_summary(batch, k=0, axs=None)
¶
Three-panel batch overview: plan cost, sparsity, and feasibility.
Creates its own figure when axs is None and returns the array of three
axes (unlike the single-axes functions, which return one ax).
Source code in src/treecf/viz_batch.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |