Decision trees
Learn a flowchart of yes/no questions: greedy splits, impurity, depth and pruning.
Animated explanation
The idea
A decision tree is a flowchart: Is income below ₹40k? If yes, is the loan longer than 5 years?… until it reaches a leaf that gives the answer. Each question looks at one feature and a threshold, so the tree slices the feature space into boxes and predicts the majority class (or mean value) in each box.
Training is greedy: at each node, try every feature and threshold, and keep the split that makes the two children purest. Repeat inside each child. Trees need no feature scaling, handle mixed feature types, capture interactions, and are easy to read. Their weakness is variance: a deep tree memorises its training data, and a small change in the data can grow a very different tree.
Purity measures
Gini impurity is the probability that two random members of a node disagree; entropy measures uncertainty in bits. Both are zero for a pure node and largest for an even mix, and in practice they pick nearly the same splits. For regression, impurity is the variance of the target in the node, and leaves predict the mean.
Controlling overfitting
Grown until every leaf is pure, a tree reaches zero training error and generalises poorly. Limit it with max_depth, min_samples_leaf or min_impurity_decrease (pre-pruning), or grow it fully and cost-complexity prune it back, choosing the penalty in by cross-validation. Better still, average many trees: random forests and boosting (next lessons).
Strengths you only get from trees
Splits are invariant to monotone transformations of a feature (no scaling needed), categorical and missing values can be handled natively, and the path to each leaf is a human-readable rule. Feature importance is the total impurity decrease contributed by each feature (biased towards features with many split points; permutation importance is safer).
The algorithm, step by step
- 1Start with all training data at the root.
- 2For every feature and every midpoint between consecutive sorted values, compute the weighted impurity of the two children.
- 3Split on the best one if it reduces impurity and stopping rules allow; otherwise make a leaf (majority class or mean).
- 4Recurse into each child.
- 5Predict by following the questions from the root to a leaf.
The maths
Gini impurity
Entropy
Split quality
Cost-complexity pruning
Strengths and weaknesses
Strengths
- Readable rules; no scaling needed
- Handles numeric and categorical features and interactions
- Fast prediction; the base of the best tabular models
Weaknesses
- High variance: small data changes give different trees
- Greedy splits are not globally optimal
- Axis-aligned boxes approximate diagonal boundaries poorly
Reach for it when…
Check your understanding
Lambdian Lab
Grow a CART tree
A classification tree for integer labels.
- gini(y): impurity of a label array.
- best_split(X, y): (feature, threshold, cost) minimising weighted Gini over all features and midpoints between sorted unique values; None if no split lowers impurity.
- build(X, y, depth, max_depth, min_samples): nested dicts — a leaf is {"leaf": True, "value": majority} and a split is {"leaf": False, "f": j, "t": threshold, "left": …, "right": …} (left means x[j] <= t).
- predict_one(node, x) and predict(node, X).