Lawfare Group → Knowledge Institute → Lawfare Academy
← AI 301 — Machine Learning, Neural Networks and Language
Weeks 6–8
Trees, ensembles and feature interpretation
Learning objectives
- Explain and trace the mechanisms in trees, ensembles and feature interpretation.
- Apply the method to the worked example and changed-condition exercise.
- Identify assumptions, failure cases and the evidence needed to justify a result.
Current study directions: use the original Academy preparation chapters, preserved teaching and complete optional worked reasoning on LawfareGroup.com. Retained attributed readings, source exercises and historical assignment directions are optional further study. This notice supersedes earlier textbook-supported directions preserved below and in historical companions. Independent subject review and observed learner testing remain pending.
A decision tree partitions feature space with tests. Increasing depth can fit increasingly specific patterns, including noise. Restricting depth, leaf size or other complexity parameters can improve generalization. A tree's ability to fit the training data is not a guarantee that its discovered splits are stable.
Ensembles combine models. Bagging reduces some variation by fitting on resampled data; random forests add feature randomness; boosting fits a sequence that addresses remaining errors under a specified objective. These descriptions do not make every ensemble suitable for every dataset. Compare against a simple reference and account for tuning effort, runtime and explanation needs.
Feature importance requires an interpretation method and a reference distribution. Split-based importance can favor certain feature types. Permutation importance measures degradation when a feature's values are shuffled in evaluation data, but correlated features can substitute for one another. Neither establishes causation. A feature that predicts using a post-outcome field may look extremely important precisely because it leaks the answer. Examine availability, stability and plausible failure routes before celebrating importance.
Worked reasoning This one-split tree uses a strict boundary: eight hours goes left and nine goes right. It is an illustrative rule, not a learned model. If measured hours have error near eight, classifications can be unstable. A fitted tree should document how its threshold was selected and whether the split remains useful on held-out cases.
Extended teaching and laboratory
A decision tree routes a case through threshold predicates. A path can explain how that fitted tree produced a result, but does not automatically explain the real-world cause of the outcome. A feature may be predictive because it records a downstream consequence or a proxy for another process. Feature importance also depends on the fitting method and correlated alternatives.
Audit threshold boundaries with values below, equal to and above the split. Establish whether a missing-value policy is supported by the actual implementation. A readable tree can still be overfit, leaked or unfairly evaluated.
Worked problem A tree predicts late delivery if final collection_attempts>0. It achieves near-perfect retrospective accuracy. The decision is intended at shipment creation. Diagnose the result.
Step-by-step reasoning 1. Collection attempts occur after the billing or delivery process. They may encode the outcome or its consequences rather than an available shipment-time predictor.
- List every feature’s availability time. Remove or reconstruct downstream fields at the prediction boundary before fitting.
- Evaluate a revised tree on held-out cases with the same timestamp rules. The original high score does not transfer to the deployable task.
- A path explanation saying collection_attempts caused the prediction describes model routing, not proof that collection calls caused late delivery.
Learn a split, then distinguish averaging from sequential correction
A fitted tree chooses predicates from training data. A hand-written rule such as hours>8 demonstrates routing, but leaves the fitting mechanism unexplained. For binary classification, let p be the fraction of positive training labels reaching a node. Its Gini impurity is 1−p²−(1−p)²=2p(1−p). It is zero for a pure node and one half for an evenly mixed node. This is a description of the labels reaching that node, not a held-out error rate.
Consider six fictional jobs with ordered feature values [1,2,3,4,5,6] and labels [0,0,1,1,1,0]. The root has three positives, so impurity is .5. For a threshold 2.5, send x≤2.5 left. The two left labels are pure zeros, while the four right labels have positive fraction 3/4 and impurity 3/8. Weight by node sizes: (2/6)×0+(4/6)×(3/8)=1/4. The impurity decrease is 1/4. Threshold 3.5 instead produces two groups of three, each with impurity 4/9, so its weighted impurity is 4/9 and decrease 1/18. The first split is preferable under this criterion among these two candidates.
To find the best split on this feature, examine midpoints between each adjacent distinct training value. Thresholds 1.5,2.5,3.5,4.5,5.5 have weighted impurities .4,.25,4/9,.5,.4 respectively;2.5 wins here. This is a greedy local choice. Recursing can fit increasingly specific training patterns without finding a globally best bounded tree or establishing generalization. Minimum leaf size, depth limits and pruning restrict the candidates or retained structure. Tune these choices in development data. A leaf's training class fraction is a fitted probability estimate, not automatic calibration.
Resample and average
Bootstrap sampling draws training row indices with replacement. A six-row draw [0,0,2,3,3,5] includes repeated rows and excludes 1 and 4. Fit one tree on that draw and other trees on other draws. Bagging aggregates their predictions; a random forest also considers randomized feature subsets when selecting splits. For three fitted probability outputs [.2,.7,.6], their average is .5. Under the declared positive-at-.5 convention the forest predicts positive. Averaging probabilities differs from taking a majority of thresholded labels: [.49,.49,.99] averages above .5, while two of its three individually thresholded predictions are negative.
For M estimators with equal error variance σ² and equal pairwise correlationρ, the average's variance is σ²[ρ+(1−ρ)/M]. Expanding the variance of a sum proves the expression. Atρ=0 it falls toσ²/M; atρ=1 it staysσ². Diversity therefore matters, and extra copies of one identical tree do not create independent evidence. Real trees need not satisfy the equal-variance/correlation assumptions. Out-of-bag predictions use only trees whose bootstrap sample omitted that row; they do not automatically enforce customer separation or a future-time evaluation boundary.
Fit the remaining loss direction
Boosting builds an additive function sequentially. For half-squared loss, the negative derivative with respect to a prediction is the residual y−F(x). Fit a small regression tree to those residuals and update Fnew=F+ηh. For targets [1,1,5,5], a constant initial mean is 3, with residuals [−2,−2,2,2]. A stump separating the first pair from the second predicts residuals−2 and 2. Withη=.5, predictions become[2,2,4,4]. Mean squared error falls from 4to 1. Withη=1, this deliberately simple training fixture fits exactly. Neither result establishes held-out improvement. For classification the negative gradient depends on the classification loss; it is not generally the raw class label minus a thresholded prediction.
Optional changed labels and full reasoning
Change the six-job labels to[0,1,1,1,0,0]. Compare thresholds 2.5 and 3.5. At 2.5 the left fraction is 1/2 and the right fraction 1/2, so weighted impurity stays.5. At 3.5 the left fraction is 2/3 and the right 1/3; both impurities are 4/9. The latter wins among these two, though all candidate thresholds must be checked before claiming a global best split for this feature. For the boosting fixture, changeηto 1.5: predictions[0,0,6,6] have MSE 1, whileη=2 gives[−1,−1,7,7]with MSE 4. A direction that decreases loss for a small step does not certify every finite step. Finally, three identical probability predictions.7average to.7; replication changes neither the fitted decision nor the amount of independent evidence.
Optional criteria, one point each: compute weighted child impurities with the stated routing rule; distinguish local training fit from evaluation; trace bootstrap membership and probability aggregation; calculate a residual update and limit its interpretation. Four points require explained calculations and assumptions, not only selected thresholds.
Apply the idea
A tree predicts late delivery if final collection_attempts>0. It achieves near-perfect retrospective accuracy. The decision is intended at shipment creation. Diagnose the result.
A split is hours>8. Evaluate 8, 8.01 and missing. Why is a missing policy necessary even when every development row had a value?
Read the worked reasoning
Eight routes to the not-greater branch; 8.01 routes to the greater branch. Missingness is not determined by those comparisons alone and requires the implementation’s explicit policy or rejection. Deployment can introduce missing values not seen in development. Test the actual library’s behavior rather than assuming a universal tree rule.
Check understanding
What does high feature importance fail to prove?
- That changing the feature would cause the predicted outcome to change
Feedback
Causal intervention requires stronger identification assumptions.
- That the model uses information from the feature
Feedback
Importance methods do describe some form of model reliance.
- That shuffling may affect the evaluated score
Feedback
Permutation methods specifically assess a score change under shuffling.
Further reading
Current study directions: use the original Academy preparation chapters, preserved teaching and complete optional worked reasoning on LawfareGroup.com. Retained attributed readings, source exercises and historical assignment directions are optional further study. This notice supersedes earlier textbook-supported directions preserved below and in historical companions. Independent subject review and observed learner testing remain pending.
Use the preserved original investigation and connected Academy preparation/application below. Compare your reasoning with its optional complete model. Historical source directions remain in immutable edition4.