Sub topic 1
Decision trees as the building block
Prerequisites
This chapter assumes you know the following:
- Basic supervised learning: features, labels, and the split into training and test sets.
- Python and NumPy basics.
- Accuracy and mean squared error.
You do not need to know how splits are chosen. The next chapter covers that.
What a decision tree is
A decision tree is a model that predicts by asking a series of yes/no questions about the features. Each question compares one feature with a threshold, such as "is mean radius less than or equal to 14.0?". The answer sends the sample down one of two branches, and the process repeats until the sample reaches a leaf.
The parts of a tree have fixed names:
- A root node is the first question. It sees every training sample.
- An internal node is any later question. It sends samples to a left or right child.
- A leaf is a node with no question. It stores the prediction.
For classification, a leaf predicts the majority class of the training samples that reached it. For regression, a leaf predicts their mean target value.
How a tree makes a prediction
Prediction is a walk from the root to a leaf. At each node the tree tests one feature and follows one branch. The leaf it ends in gives the output. Because each step is a single comparison, prediction is fast. It takes about as many comparisons as the tree is deep.
Training works in the opposite direction. The algorithm starts with all the training data at the root. It picks a question that separates the data well, splits the data into two groups, and repeats on each group. This is called recursive partitioning. It stops when a node is pure (all samples share one label), when a limit such as maximum depth is reached, or when no split is possible.
A first tree in scikit-learn
The code below trains a tree and prints its rules as text.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, export_text
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(
data.data, data.target, test_size=0.2, random_state=0, stratify=data.target
)
tree = DecisionTreeClassifier(max_depth=2, random_state=0)
tree.fit(X_train, y_train)
print(export_text(tree, feature_names=list(data.feature_names)))
print("Train accuracy:", tree.score(X_train, y_train))
print("Test accuracy: ", tree.score(X_test, y_test))With max_depth=2, the tree has at most four leaves, so you can read the whole model. Each printed line is one question, and each indented block is one branch.
What trees do well
- They need little preparation. Trees do not require feature scaling, because each split compares a single feature with a threshold.
- They capture interactions. A question deeper in the tree applies only to samples that passed the earlier questions, so the effect of one feature can depend on another.
- They handle non-linear relationships. The prediction is a step function of each feature, which can follow curved patterns.
- They are readable. A short tree can be explained to someone who has no machine learning background.
The weakness that motivates forests
Without a depth limit, a tree keeps splitting until every leaf is pure. Such a tree can fit the training set perfectly, including its noise. Its training accuracy is then 1.0, but its test accuracy is lower. A small change in the training data can also produce a quite different tree, because an early split changes everything below it.
This sensitivity is called high variance. A random forest addresses it by training many trees and combining them. Later chapters explain how split choice works, why the variance arises, and how averaging reduces it.
Exercise
Run the code above with max_depth=None and compare the train and test accuracy with the depth-2 result.
Exercise
A decision tree is trained with no depth limit and reaches 100% training accuracy but noticeably lower test accuracy. What does this indicate, and why is a single tree prone to it?
Show answer
It indicates overfitting. The tree keeps splitting until every leaf is pure, so it memorises noise in the training set. Its structure also depends heavily on the particular training sample, so it has high variance. Averaging many such trees, as a random forest does, reduces that variance.