Sub topic 1
Anatomy of a decision tree
Prerequisites
This chapter assumes you know basic Python and the supervised learning setup: features, labels, and training and test sets. You should also know the difference between classification and regression, and be comfortable with averages and simple probabilities. No scikit-learn experience is needed yet.
The parts of a tree
A decision tree predicts by asking a sequence of simple questions about an input's features. Each question has a yes/no answer, and each answer leads to the next question or to a final prediction. The structure is a tree of connected nodes.
- Root node: the first question. Every prediction starts here.
- Internal node: a question that sends the sample down one of two branches. Each internal node stores one feature and one threshold, such as
petal length <= 2.45. - Branch: the outcome of a question. In a binary tree (the kind scikit-learn builds), the left branch means the test is true and the right branch means it is false.
- Leaf node: a node with no question. It holds the prediction.
A leaf for classification stores the class counts of the training samples that reached it. The predicted class is the most common one. For regression, a leaf stores the mean target value instead.
Tracing a prediction
To predict, start at the root and apply each node's test to the sample. Follow the matching branch until you reach a leaf, then return that leaf's value. The path from the root to a leaf is a chain of conditions joined by "and", so each leaf corresponds to one rule.
Take a flower with petal length 4.5 and petal width 1.3:
- At the root,
4.5 <= 2.45is false, so go right. - At the internal node,
1.3 <= 1.75is true, so go left. - The leaf says versicolor, so that is the prediction.
The same logic works as plain Python. This function hard-codes the tree above:
def predict_iris(petal_length, petal_width):
if petal_length <= 2.45:
return "setosa"
if petal_width <= 1.75:
return "versicolor"
return "virginica"
print(predict_iris(4.5, 1.3)) # versicolor
print(predict_iris(1.4, 0.2)) # setosa
print(predict_iris(5.8, 2.2)) # virginicaA fitted tree is this kind of nested if statement. The learning algorithm's job is to find the questions and thresholds automatically from data.
Depth, size, and regions
Two numbers describe a tree's size. The depth of a node is the number of branches between it and the root, and the tree's depth is that of its deepest leaf. The tree above has depth 2 and three leaves. The number of leaves is a useful measure of complexity, because each leaf is one rule the tree can express.
Each leaf also owns a region of feature space. With numeric features, every question cuts space along one axis, so the regions are axis-aligned rectangles (boxes in higher dimensions). The tree predicts one constant value for every point inside a box. Diagonal boundaries must be approximated with several steps, which later chapters return to as a limitation.
Common mistakes
- Assuming a leaf is always pure. A leaf holds a mix of classes unless the tree was grown until every leaf contains one class. Check the stored counts before trusting a leaf.
- Mixing up branch direction. In scikit-learn, left means the test
feature <= thresholdis true. Reading it backwards flips the rule. - Treating the tree as learned by hand. The questions are not chosen by a person. The next chapter shows how the algorithm picks them.
Exercise
Using the tree in this chapter, what does it predict for a flower with petal length 5.1 and petal width 1.9? Name the root-to-leaf rule that produced the prediction.
Show answer
It predicts virginica. At the root, 5.1 <= 2.45 is false, so go right. At the internal node, 1.9 <= 1.75 is false, so go right to the virginica leaf. The rule is: petal length > 2.45 and petal width > 1.75.