Trees as learned decision rules
A tree learns readable local rules; constraints keep those rules from becoming a lookup table.
A marketplace must catch rare fraud without blocking trustworthy sellers.
This chapter moves from enumerate useful cuts to audit the learned rules. We will make the idea concrete in a small code example before placing it back inside a production system with reviewer capacity plus false-decline cost.
Inspect feature use, leaf support, missing routing, and suspicious splits on IDs or post-outcome fields.
Verify the behavior illustrated in gini_split.py, then measure decision utility, slice reliability, and system latency.
High-cardinality IDs and random row splitting produce spectacular leakage that XGBoost exploits. An unsafe release must degrade to a named baseline or the last known-good version.
Build the idea one layer at a time.
A decision tree learns a sequence of questions such as “amount ≤ 40?” and turns the answers into piecewise-constant predictions.
At each node, training greedily searches feature thresholds for the largest reduction in impurity or loss. The split is locally best, not globally optimal, and nearby input values can receive abrupt different predictions when they land on opposite sides.
Unrestricted depth can memorize noise, rare categories, and identifiers. Minimum leaf size, depth limits, pruning, temporal validation, and stable missing-value policy are therefore part of the model—not cleanup after training.
- impurity gain
- bagging
- feature subsampling
- pseudo-residuals
Follow the reasoning before touching the code.
- 01
Enumerate useful cuts
For each feature, test thresholds between observed values and compute weighted child impurity relative to the parent.
- 02
Stop before memorization
Constrain depth, leaf population, or impurity gain using validation performance and slice stability.
- 03
Audit the learned rules
Inspect feature use, leaf support, missing routing, and suspicious splits on IDs or post-outcome fields.
rows = [(10, 0), (20, 0), (35, 1), (50, 1), (80, 1)]
def gini(labels):
if not labels:
return 0.0
positive_rate = sum(labels) / len(labels)
return 1 - positive_rate ** 2 - (1 - positive_rate) ** 2
for threshold in [15, 27.5, 42.5, 65]:
left = [label for value, label in rows if value <= threshold]
right = [label for value, label in rows if value > threshold]
weighted = (len(left) * gini(left) + len(right) * gini(right)) / len(rows)
print(threshold, round(weighted, 3), left, right)Score candidate thresholds by weighted Gini impurity.
What to notice
- Threshold 27.5 creates two pure children and therefore zero weighted impurity on this tiny dataset.
- A perfect training split says nothing about future stability when the sample is small or the feature leaks the outcome.
YOUR TURN Pause and predict before revealing the answer.
Add one negative label at value 80. Does the 27.5 split stay perfectly pure?
Need a hint?
Inspect the labels in the right child after adding the row.
Check your reasoning
No. The right child now contains a negative among positives, so its Gini impurity becomes greater than zero.
Classical baselines
Connect the chapter to the course’s quantitative spine.
A tree learns readable local rules; constraints keep those rules from becoming a lookup table. The stage-wise boosting update below is the shared course-level lens that helps quantify the surrounding system. Name each symbol, check its units or shape, and use only values that were available at the production cutoff.
Stage-wise boosting update
| Symbol | Meaning / shape / unit |
|---|---|
F_m | ensemble after stage m |
h_m | new weak learner |
η | learning rate |
Open derivation and numerical substitution
Start from the production quantity being optimized, substitute the observed values with their declared units, then isolate the model-controlled term. Preserve shape annotations at each step so broadcasting or aggregation cannot silently change the result.
- Write the named inputs: F_m, h_m, η.
- Substitute one small, hand-checkable batch before vectorizing.
- Calculate an independent reference value and compare within a declared tolerance.
# equation → code contract
inputs = validate_shapes_and_units(batch)
value = compute_c05(inputs)
assert is_finite(value)Change one assumption and make the tradeoff visible.
Use the worked chapter as your prediction, then change one input in the lab and explain why the output moves. The lab runs predefined TypeScript and never executes learner code, so the focus stays on the concept rather than setup.
Tree capacity lab
Grow depth and compare training fit with estimated temporal generalization.
Assumption: Estimate calibrated to a small, imbalanced synthetic tabular dataset.
Open nonvisual data table
| Item | Computed state | Interpretation |
|---|---|---|
| fold 1 | 70.3% | stable |
| fold 2 | 69.6% | stable |
| fold 3 | 68.9% | stable |
| fold 4 | 68.2% | stable |
| fold 5 | 67.5% | stable |
Trace the complete operating path.
Carry the chapter’s central idea into the system: A tree learns readable local rules; constraints keep those rules from becoming a lookup table. The path below shows where that reasoning needs an artifact, a test, an owner, or a fallback.
- 01
Turn “Enumerate useful cuts” into a written invariant and a failing test.
- 02
Promote gini_split.py from illustration to a versioned implementation with representative fixtures.
- 03
Use “Audit the learned rules” as the release checkpoint: Inspect feature use, leaf support, missing routing, and suspicious splits on IDs or post-outcome fields.
- 04
Serve or materialize behind explicit reviewer capacity plus false-decline cost.
- 05
Join telemetry to mature outcomes and retain a rollback path.
Observability
Join service health, input quality, prediction distributions, slice behavior, and mature outcomes by exact version.
Cost
Measure storage, preprocessing, compute, queueing, and human review under a representative arrival pattern.
Failure modes
High-cardinality IDs and random row splitting produce spectacular leakage that XGBoost exploits. Add a detector, owner, mitigation, and stop condition for this class of failure.
Alternatives
Compare a rule, a simpler statistical baseline, and a different system boundary before adding model complexity.
Explain the contract, not just the vocabulary.
Rare-fraud ensemble decision system
Compare a tree, forest, gradient boosting, and XGBoost with temporal validation and operational thresholding.
- Ensemble ablations
- Cost and capacity analysis
- Calibration and explanations
- Latency/fallback plan
Read primary material with a purpose.
Return to the opening failure.
Version missing-value behavior and model format; monitor calibration, explanations, latency, and reviewer yield.
For this lesson, the release evidence is the worked code example, a hand-checked formal result, deterministic simulation output, a ≥80% checkpoint, the production rubric, and a named fallback. The course resolves when the system can produce calibrated random-forest and xgboost baselines with cost-sensitive thresholds.