Decision Trees


In-depth Articles

Introduction

Decision Trees are a non-parametric supervised learning method used for both classification and regression tasks. Unlike parametric models, they make no assumptions about the underlying data distribution.

A decision tree has a hierarchical structure composed of:

The key appeal of decision trees lies in their interpretability: the model can be visualized as a flowchart of if-then rules, making it easy to understand and explain predictions. They require no feature scaling, handle both numerical and categorical features, and naturally capture non-linear relationships and feature interactions.




Classification Trees (CART)

The CART (Classification and Regression Trees) algorithm builds classification trees via recursive binary splitting. At each internal node, the algorithm searches for the best split — a feature \(j\) and a threshold \(s\) — that minimizes an impurity measure in the resulting child nodes.

Impurity Measures

Let \(p_k\) denote the proportion of observations belonging to class \(k\) in a given node, with \(K\) total classes.

a) Gini Index

The Gini Index measures the probability of misclassifying a randomly chosen element:

\[ G = \sum_{k=1}^{K} p_k (1 - p_k) = 1 - \sum_{k=1}^{K} p_k^2 \]

The Gini Index equals 0 when all observations belong to a single class (pure node) and reaches its maximum when classes are equally distributed.

b) Entropy (Information Gain)

Entropy quantifies the disorder or uncertainty in a node:

\[ H = -\sum_{k=1}^{K} p_k \log_2(p_k) \]

Like the Gini Index, entropy is 0 for a pure node and maximal for uniform class distributions.

c) Misclassification Rate

The simplest impurity measure:

\[ E = 1 - \max_k(p_k) \]

This measure is less sensitive to changes in class probabilities and is therefore less commonly used for tree growing (but useful for pruning).

Information Gain

The quality of a split is evaluated via Information Gain, defined as the reduction in impurity from parent to children:

\[ \text{Information Gain} = H(\text{parent}) - \sum_{c \in \{L, R\}} \frac{n_c}{n_{\text{parent}}} H(\text{child}_c) \]

where \(n_c\) is the number of observations in child node \(c\) and \(n_{\text{parent}}\) is the total number in the parent node. The split that maximizes Information Gain (or equivalently, minimizes the weighted child impurity) is selected.




Regression Trees

For regression tasks, the tree predicts a continuous value. The feature space is partitioned into \(J\) distinct regions \(R_1, R_2, \ldots, R_J\), and for each region the prediction is the mean of the training observations falling in that region:

\[ \hat{y}_{R_j} = \frac{1}{|R_j|} \sum_{i \in R_j} y_i \]

The objective is to find the partition that minimizes the Residual Sum of Squares (RSS):

\[ \text{RSS} = \sum_{j=1}^{J} \sum_{i \in R_j} (y_i - \hat{y}_{R_j})^2 \]

At each split, we choose the feature \(j\) and threshold \(s\) that define two half-planes:

\[ R_1(j,s) = \{X \mid X_j \leq s\} \quad \text{and} \quad R_2(j,s) = \{X \mid X_j > s\} \]

and we seek \(j\) and \(s\) that minimize:

\[ \sum_{i: x_i \in R_1(j,s)} (y_i - \hat{y}_{R_1})^2 + \sum_{i: x_i \in R_2(j,s)} (y_i - \hat{y}_{R_2})^2 \]




The Splitting Algorithm

Finding the globally optimal tree partition is computationally infeasible (NP-hard). Therefore, decision trees use a greedy top-down approach known as recursive binary splitting:

  1. Starting at the root, consider all features \(j = 1, \ldots, p\) and all possible thresholds \(s\) for each feature.
  2. For each candidate split \((j, s)\), compute the impurity reduction (Information Gain for classification, RSS reduction for regression).
  3. Select the \((j^*, s^*)\) pair that maximizes the impurity reduction.
  4. Split the node into two child nodes based on whether \(X_j \leq s\) or \(X_j > s\).
  5. Repeat recursively on each child node until a stopping criterion is met (e.g., minimum samples per leaf, maximum depth, or no further impurity reduction possible).

This greedy procedure does not guarantee a globally optimal tree but is computationally tractable with complexity \(O(n \cdot p \cdot \log n)\) per split when features are pre-sorted.




Pruning

A fully grown tree (one that perfectly classifies training data) will typically overfit — it captures noise in the training set and generalizes poorly to unseen data. Pruning addresses this by reducing the tree's complexity.

Cost-Complexity Pruning (Weakest Link Pruning)

Rather than using early stopping criteria, the preferred approach is to grow a large tree \(T_0\) and then prune it back. Cost-complexity pruning defines a penalized objective:

\[ C_\alpha(T) = \sum_{m=1}^{|T|} \sum_{i \in R_m} (y_i - \hat{y}_m)^2 + \alpha |T| \]

where:

  • \(|T|\) is the number of terminal nodes (leaves) in the subtree \(T\)
  • \(\hat{y}_m\) is the prediction for region \(R_m\)
  • \(\alpha \geq 0\) is the complexity parameter that controls the trade-off between tree size and goodness of fit

When \(\alpha = 0\), the full tree \(T_0\) is optimal. As \(\alpha\) increases, there is a penalty for having too many leaves, leading to smaller subtrees.

Selecting \(\alpha\) via Cross-Validation

The optimal \(\alpha\) is selected using k-fold cross-validation:

  1. Divide the training data into \(k\) folds.
  2. For each fold, grow a full tree on the remaining \(k-1\) folds and compute the sequence of subtrees for a range of \(\alpha\) values.
  3. Evaluate prediction error on the held-out fold for each \(\alpha\).
  4. Choose \(\alpha\) that minimizes the average cross-validated error.
  5. Return the subtree from the full training set corresponding to the chosen \(\alpha\).



Advantages and Limitations

Advantages

  • Interpretability: easily visualized and explained to non-technical stakeholders
  • Handles mixed feature types: works with both numerical and categorical variables without encoding
  • No feature scaling required: invariant to monotone transformations of features
  • Captures interactions: naturally models feature interactions through the tree structure
  • Handles missing data: surrogate splits can deal with missing values
  • Fast inference: prediction time is \(O(\log n)\) — just traverse the tree

Limitations

  • High variance: small changes in the training data can produce a completely different tree (instability)
  • Tendency to overfit: without pruning, trees memorize training noise
  • Axis-aligned splits only: decision boundaries are perpendicular to feature axes, making it difficult to capture diagonal relationships
  • Greedy and suboptimal: the top-down greedy approach does not guarantee a globally optimal partition
  • Bias toward features with many levels: features with more unique values have more candidate splits and may be unfairly favoured



From Single Trees to Ensembles

The high variance and instability of individual decision trees motivates the development of ensemble methods that combine multiple trees to produce more robust predictions:

The fundamental insight is that while a single tree is a weak learner with high variance, an ensemble of many trees can achieve the performance of a strong learner by averaging out individual errors:

\[ \text{Var}\left(\frac{1}{B}\sum_{b=1}^{B} T_b(x)\right) = \rho \sigma^2 + \frac{1-\rho}{B}\sigma^2 \]

where \(\rho\) is the pairwise correlation between trees and \(\sigma^2\) is the variance of a single tree. As \(B \to \infty\), the second term vanishes, and reducing \(\rho\) (via random feature subsets) directly reduces ensemble variance.