A decision tree is a non-parametric supervised learning algorithm, which is utilized for both classification and regression tasks. As in linear regression, it takes in a table with many columns (features), and one column is designated a label. It generates a hierarchical, tree structure from this table, which consists of a root node, internal nodes and leaf nodes.
- Internal nodes are known as decision nodes
- Leaf nodes represent the classification of the dataset.
In a decision tree
- Root node and each internal node asks a specific question
- Directs us towards a child of that node depending on the answer we give.
- At the end, we reach a leaf node which declares our class (ie, label).
As mentioned before, a decision tree can be generated for both regression or classification.
- When a decision tree classifies things into categories, it is called a classification tree.
- When a decision tree predicts a numerical value, it is a regression tree.
The area where they really shine is classification and tabular data (In this context, see Why Do Tree Based-Models Outperform Neural Nets on Tabular Data?). Therefore we will first investigate classification trees. Then we will investigate the regression trees.
CLASSIFICATION TREES
Problem type: We got a table with categorical data in all its features/columns. We try to derive a column (ie, the label column) from the other columns (ie, feature columns).
Caveat: we assume that other columns carry some info about the column to be derived. For example, we cannot derive e person’s name from age, occupation, height, weight.
We will use Gini index as a splitting criterion. Gini index (CART) is best explained in the following video: The Gini Impurity Index explained in 8 minutes!
GINI(D): # here D is Dleft or Dright. Calculations are done on the label column.
impurity = 1
for each distinct category c appearing in the label column of D:
p = number of rows with label c / number of rows in D
impurity = impurity - p²
return impurity
BUILD_TREE(D, depth):
majority_class = most frequent label in D
# Break ties using a fixed rule.
if all labels in D are identical
or depth >= MAX_DEPTH
or number of rows in D < MIN_SAMPLES_SPLIT:
return LEAF(majority_class)
best_impurity = infinity
best_split = NONE
for each feature Xj:
C = distinct categories of Xj appearing in D
for each distinct partition of C into two nonempty sets S and C\S:
# Examine each partition only once:
# S | C\S and C\S | S are the same partition.
Dleft = rows of D whose Xj value belongs to S
Dright = remaining rows of D
NL = number of rows in Dleft
NR = number of rows in Dright
N = NL + NR
split_impurity =
(NL / N) * GINI(Dleft)
+ (NR / N) * GINI(Dright)
if split_impurity < best_impurity:
best_impurity = split_impurity
best_split = (Xj, S, Dleft, Dright)
if best_split is NONE:
return LEAF(majority_class)
gain = GINI(D) - best_impurity
if gain < MIN_GAIN:
return LEAF(majority_class)
retrieve (Xj, S, Dleft, Dright) from best_split
node = INTERNAL_NODE(question: "Is Xj in S?")
node.left = BUILD_TREE(Dleft, depth + 1)
node.right = BUILD_TREE(Dright, depth + 1)
return node
PREDICT(row, tree):
node = tree
while node is not a leaf:
if row[node.feature] belongs to node.category_set:
node = node.left
else:
node = node.right
return node.class_label
We have used C5 for information gain. There is also ID3 and C4.5
CHAID (Chi-Squared Automatic Interaction Detector)
- Splitting Criterion: Chi-square tests (for categorical targets) or F-tests (for continuous targets).
- Multi-way Splits: Can create >2 branches per node.
- Use Case: Often used in marketing research.
- Start at the Root Node
- Input: Dataset D with categorical features X1,X2,…,Xp and categorical target Y.
- Goal: Split D into statistically significant subsets using chi-square tests (classification) or F-tests (regression).
- Choose the Best Split
For each categorical feature Xj:
- Cross-tabulate Xj vs. target Y into a contingency table.
- Compute chi-square statistic (for classification):
χ2=∑(Oij−Eij)2Eijχ2=∑Eij(Oij−Eij)2
where OijOij= observed count, EijEij= expected count under independence.
- Merge non-significant categories:
-
-
- If categories of XjXjhave statistically similar distributions of YY(p-value > α, e.g., 0.05), merge them.
-
- Select the feature XjXjwith the smallest p-value (most significant association with YY).
- Create Child Nodes
- Partition DDinto subsets for each remaining category of XjXj(after merging).
- Recursively apply Steps 2–3 to each subset until:
- No significant splits remain (p-value > α).
- Max depth reached.
- Node size < minimum threshold.
- Assign Leaf Nodes
- Assign the most frequent class (classification) or mean value (regression).
MARS (Multivariate Adaptive Regression Splines)
- Hybrid Approach: Combines decision trees with linear regression splines.
- Best for: Regression problems with complex interactions.
- Start with the Root Model
- Input: Dataset D with numerical features X1,X2,…,Xp and target Y (regression only).
- Initial Model: Y=β0Y=β0(mean of YY). No splits yet.
- Forward Pass: Add Basis Functions (Splines)
For each feature XjXj:
- Identify candidate knots (breakpoints) for piecewise linear terms:
-
-
- Sort unique values of XjXj, evaluate midpoints between adjacent values as potential knots tt.
-
- Create hinge functions for each knot tt:
h+(Xj,t)=max(0,Xj−t)(right hinge)h+(Xj,t)=max(0,Xj−t)(right hinge)h−(Xj,t)=max(0,t−Xj)(left hinge)h−(Xj,t)=max(0,t−Xj)(left hinge)
- Fit all candidates and keep the one that most reduces residual error (e.g., RSS).
- Update model by adding the best hinge function:
Y=β0+β1⋅h(Xj,t)Y=β0+β1⋅h(Xj,t)
- Repeat until max terms reached or no improvement.
- Backward Pass: Prune Basis Functions
- Remove the least important terms (smallest contribution to error reduction).
- Use Generalized Cross-Validation (GCV) to balance fit and complexity:
GCV=RSS(1−effective_terms/N)2GCV=(1−effective_terms/N)2RSS
- Final Model
- Linear combination of hinge functions:
Y=β0+∑βihi(Xj,t)Y=β0+∑βihi(Xj,t) - No “leaf nodes” (MARS is a regression model, not a tree).
REGRESSION TREES
VARIANCE(D):
# Calculations use only the label column of D.
mean_label = mean of the label column of D
impurity = 0
for each row in D:
impurity = impurity + (row.label - mean_label)²
return impurity / number of rows in D
BUILD_TREE(D, depth):
mean_label = mean of the label column of D
if all labels in D are identical
or depth >= MAX_DEPTH
or number of rows in D < MIN_SAMPLES_SPLIT:
return LEAF(prediction = mean_label)
best_impurity = infinity
best_split = NONE
for each feature Xj:
V = sorted distinct values of Xj appearing in D
for each consecutive pair V[k], V[k+1]:
t = (V[k] + V[k+1]) / 2
Dleft = rows of D where Xj <= t
Dright = rows of D where Xj > t
NL = number of rows in Dleft
NR = number of rows in Dright
N = NL + NR
split_impurity =
(NL / N) * VARIANCE(Dleft)
+ (NR / N) * VARIANCE(Dright)
if split_impurity < best_impurity:
best_impurity = split_impurity
best_split = (Xj, t, Dleft, Dright)
if best_split is NONE:
return LEAF(prediction = mean_label)
gain = VARIANCE(D) - best_impurity
if gain < MIN_GAIN:
return LEAF(prediction = mean_label)
retrieve (Xj, t, Dleft, Dright) from best_split
node = INTERNAL_NODE(feature = Xj, threshold = t)
node.left = BUILD_TREE(Dleft, depth + 1)
node.right = BUILD_TREE(Dright, depth + 1)
return node
PREDICT(row, tree):
node = tree
while node is not a leaf:
if row[node.feature] <= node.threshold:
node = node.left
else:
node = node.right
return node.prediction
# Train the tree:
tree = BUILD_TREE(training_data, 0)
# Predict the numerical label of a new row:
predicted_label = PREDICT(new_row, tree)