Decision Trees: Splits, Hyperparameters, and Ensembles
Machine LearningModel answerLast updated
Explain how decision trees partition data, how splits are scored for classification vs regression, how hyperparameters control overfitting, and how ensemble methods improve on a single tree.
Background
Decision trees are foundational to many of the strongest models used on tabular data today. Understanding them from first principles — how they split, what they optimize, and why they overfit — is a prerequisite for reasoning about the ensemble methods built on top of them.
Questions
- Walk through how a decision tree learns a model from training data, for both classification and regression tasks.
- What objective does the tree optimize when choosing a split? Describe the criterion used for classification and for regression, and explain why misclassification error is not a good choice for growing a classification tree.
- Name at least four hyperparameters that control tree complexity and describe the direction of their effect on bias and variance.
- Choose one ensemble method that builds on decision trees — such as a random forest or a gradient boosted tree model — explain the mechanics, and describe the conditions under which you would prefer it over a single tree, and over the other ensemble.
What is expected
A strong answer explains the mechanics concretely (not just names and buzzwords), includes at least one formula or worked example for the splitting criterion, and discusses trade-offs rather than declaring a single winner. For the ensemble question, go beyond "it reduces overfitting" and explain why at an algorithmic level.
Hints
Hint 1
For the splitting criterion, try writing out the weighted impurity formula for a binary split. Then plug in a simple example — say, 10 samples split into two groups of 5 — to show the gain is nonzero even when the split looks balanced.
Hint 2
For hyperparameters, organize your answer around what part of the tree they constrain (depth, leaf size, complexity penalty) and whether they act before or after the tree is grown.
Hint 3
For ensembles, the key word for random forests is *decorrelation* and for gradient boosting it is *sequential residual fitting*. If you can explain what problem each mechanic is solving, the trade-off between the two methods follows naturally.
Model answer
Easy Try it yourself first — then compare your approach, complexity and edge cases.
1. How a decision tree works
A decision tree recursively partitions the feature space into axis-aligned rectangular regions and makes a constant prediction in each region.
Training is greedy and top-down. Starting from the root, which contains all training examples, the algorithm tries every candidate split of the form x_j ≤ t for numeric features. It picks the split that produces the largest improvement in a chosen impurity measure, divides the data into left and right child nodes, and repeats the process at each child. Recursion stops when a stopping criterion fires — maximum depth reached, minimum node size, or no split with positive gain.
Prediction routes a new sample down the tree by following split rules from root to leaf. Classification leaves return either the majority class or class proportions as probabilities. Regression leaves return the mean of their training targets (under squared error) or the median (under absolute error).
Finding the globally optimal tree is NP-hard, so CART uses a locally greedy approach with no lookahead. This is fast but means the tree can miss splits that are only useful in combination. No feature scaling is required, and the tree naturally handles nonlinear relationships and feature interactions.
2. Choosing a split
For a node holding sample set S, a candidate split produces left child L and right child R. The chosen criterion is maximized:
$$\Delta = I(S) - \frac{|L|}{|S|} I(L) - \frac{|R|}{|S|} I(R)$$
Classification.
- Gini impurity: $I = 1 - \sum_k p_k^2$, where $p_k$ is the fraction of class $k$ in the node. It measures the probability of mislabeling a randomly drawn sample under the node's class distribution. This is the sklearn/CART default.
- Entropy: $I = -\sum_k p_k \log_2 p_k$. The gain from a split is called information gain. C4.5 divides this by the split's own entropy (gain ratio) to avoid favoring features with many categories.
In practice, Gini and entropy produce nearly identical trees. Gini is slightly faster because it avoids a log computation.
Why not misclassification error? Misclassification error is $1 - \max_k p_k$. It is not strictly concave, so it often assigns a gain of zero to splits that Gini or entropy would rank as clearly useful. For example, splitting a 50/50 node into two 75/25 nodes reduces impurity under both Gini and entropy but leaves misclassification error unchanged at 0.5 → 0.25, wait — actually the parent is 0.5 and each child is 0.25, so there is a gain here, but cases exist where the metric is flat across multiple split candidates, giving the greedy algorithm no gradient to follow. It is sometimes used as a stopping criterion but not for growing.
Worked example (Gini). Parent: 5 positive, 5 negative, $I = 0.5$. Split into L: 4+/1− and R: 1+/4−.
$$I(L) = 1 - (0.8^2 + 0.2^2) = 0.32 \qquad I(R) = 0.32$$
$$\Delta = 0.5 - \frac{5}{10}(0.32) - \frac{5}{10}(0.32) = 0.18$$
The gain is positive, so this split is selected over any candidate with $\Delta < 0.18$.
Regression. The standard criterion is variance reduction (MSE): $I(S) = \frac{1}{|S|}\sum_{i \in S}(y_i - \bar{y})^2$. Maximizing the impurity decrease is equivalent to minimizing total sum of squared errors across the two children. Mean absolute error with median predictions is more robust to outliers but slower to compute.
3. Hyperparameters and bias/variance
A fully grown tree has low bias and very high variance — small changes in training data can completely flip the root split. Most hyperparameters are regularizers that trade a small amount of bias for a large reduction in variance.
| Hyperparameter | Effect when tightened |
|---|---|
max_depth | Shallower tree, higher bias, lower variance. Also limits the order of interactions the tree can model. |
min_samples_leaf | Larger leaves smooth predictions and produce more reliable probability estimates. |
min_impurity_decrease | Skips splits whose gain falls below a threshold (pre-pruning). |
ccp_alpha | Post-pruning via cost-complexity: minimizes $R(T) + \alpha |
max_features | Considers a random subset of features per split. Primarily useful in ensembles for decorrelating trees. |
A common mistake is tuning max_depth alone. In practice, min_samples_leaf is often more effective because it directly controls the reliability of leaf predictions rather than just the number of decisions.
4. Ensemble methods
Random Forest (bagging + feature subsampling)
Train $B$ trees independently, each on a bootstrap resample of the training set. At each split, only $\sqrt{d}$ randomly chosen features are eligible (for classification). Predictions are averaged (regression) or probability-averaged (classification).
The variance of the average of $B$ correlated trees with individual variance $\sigma^2$ and pairwise correlation $\rho$ is:
$$\rho \sigma^2 + \frac{1 - \rho}{B} \sigma^2$$
Bagging drives the second term toward zero as $B$ grows. Feature subsampling reduces $\rho$, shrinking the first term. Bias stays roughly the same as a single deep tree. The trees train in parallel and are robust to noise because no single tree's error drives the ensemble. Adding more trees never causes overfitting — it only adds compute.
Gradient Boosted Trees (XGBoost, LightGBM)
Boosting builds an additive model sequentially:
$$F_m(x) = F_{m-1}(x) + \eta , h_m(x)$$
At each step, compute pseudo-residuals $r_i = -\partial L(y_i, F(x_i)) / \partial F$ — for log-loss these are $y_i - \hat{p}_i$ — and fit a shallow tree $h_m$ to those residuals. The new tree is added at a reduced learning rate $\eta$. Modern implementations use Newton steps for leaf values, adding a second-order term for faster convergence.
Where random forests reduce variance, boosting primarily reduces bias by iteratively correcting errors. It can overfit, so the main regularization levers are a small learning rate (0.01–0.1) paired with early stopping, shallow trees (depth 3–8), and row/column subsampling.
Which to choose
A single tree is useful only when interpretability is critical and accuracy can be sacrificed. For most tabular problems it is clearly dominated by both ensembles.
Between the two ensembles: prefer random forest when you need a robust baseline quickly, have noisy labels, or want to avoid careful tuning — it is hard to badly misconfigure. Prefer gradient boosting when you need maximum predictive performance on clean data and are willing to tune the learning rate, tree size, and regularization parameters. On structured tabular problems, well-tuned GBMs typically outperform random forests, but the gap depends heavily on data size and label quality.
A common mistake is treating ensemble methods as black boxes and skipping residual analysis or feature importance checks. Both methods support feature importance (impurity-based or permutation-based), and inspecting them is a useful sanity check that the model is using meaningful signals.