AI 301 — Machine Learning, Neural Networks and Language · Weeks 6–8
Trees, ensembles and feature interpretation
Full teaching sequence and cumulative subject work
Study the complete on-site source sections below, then work the original example, changed-condition problem and transfer laboratory. Study scikit-learn Decision Trees, Ensembles and Permutation Feature Importance. Compare a shallow tree with a regularized linear model on the same development splits.
Trees, leakage and explanations of a decision
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.
Trace the complete reasoning
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.
Independent transfer problem
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?
Compare the transfer solution
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.
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.
Study the mechanism
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 example
def stump(hours):
return "review" if hours > 8 else "ordinary"
assert stump(8) == "ordinary"
assert stump(9) == "review"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.
Check your understanding
What does high feature importance fail to prove?
- That changing the feature would cause the predicted outcome to change
- That the model uses information from the feature
- That shuffling may affect the evaluated score
Answer and reasoning for every option
Correct: That changing the feature would cause the predicted outcome to change Causal intervention requires stronger identification assumptions.
Reconsider: That the model uses information from the feature Importance methods do describe some form of model reliance.
Reconsider: That shuffling may affect the evaluated score Permutation methods specifically assess a score change under shuffling.
Transfer laboratory: Correlated feature importance
Two identical columns each contain the same useful signal. Permuting one barely changes performance. Does that prove the signal is irrelevant?
Work through the full solution
No. The other column can substitute for the permuted one. Individual permutation importance depends on the model and correlations in the evaluation data. Consider grouped permutation or an explicitly designed comparison, and avoid causal claims. Also check whether the duplicated signal is available at prediction time.
Evidence of understanding
Explain substitution and recommend a correlation-aware diagnostic.
Complete on-site teaching readings
These are full attributed source sections, not previews. Read them alongside the original Academy explanation and code.
- 1.10. Decision Trees · The scikit-learn developers
- 1.11. Ensembles: Gradient boosting, random forests, bagging, voting, stacking · The scikit-learn developers
- 5.2. Permutation feature importance · The scikit-learn developers
Open the complete practice sequence and cumulative assessment →