Overview

9 Splitting data by asking questions: Decision trees

This chapter introduces decision trees as intuitive, interpretable models for both classification and regression. A decision tree predicts by asking a sequence of yes/no questions (decision nodes) and returning the label or value stored at a leaf. The structure consists of a root node, internal decision nodes, and leaf nodes, and it naturally mirrors human reasoning: partition the data by answering one question at a time. Trees can handle categorical features (by turning them into binary questions, e.g., via one-hot encoding) and continuous features (by selecting thresholds that split values), and the final prediction for a sample is the majority class (classification) or the average target (regression) at the reached leaf.

Training proceeds top-down with a greedy algorithm that picks, at each node, the split that best separates the data according to a chosen metric. For classification, the chapter compares accuracy, Gini impurity, and entropy (information gain), emphasizes weighted averages when branches differ in size, and shows how non-binary categories and numeric features translate into multiple candidate binary splits. To prevent overfitting, it introduces practical stopping criteria and hyperparameters such as maximum depth, minimum samples per split/leaf, and minimum impurity decrease, and demonstrates how to build and visualize trees with scikit-learn. For regression trees, the same top-down process is used but splits are chosen to minimize mean squared error, producing piecewise-constant predictions and axis-aligned boundaries in low dimensions.

Through worked examples—an app recommendation toy problem, a 2D geometric dataset, and a real university admissions dataset—the chapter highlights decision trees’ dual strengths: competitive predictive performance and clear interpretability (e.g., revealing which features and thresholds drive decisions). It shows how unrestricted trees can overfit and how tuning depth and sample-based constraints improves generalization. A compact regression example predicts app engagement by age with simple, readable segments. Finally, the chapter surveys impactful applications in healthcare and recommendation systems, and points ahead to ensemble methods (such as random forests and gradient-boosted trees) that combine many trees to further boost accuracy while retaining much of the interpretability and practical utility introduced here.

A decision tree used to decide whether we want to wear a jacket or not on a given day. We make the decision by traversing the tree down and taking the branch corresponding to each correct answer.
A regular decision tree with a root node, decision nodes, branches, and leaves. Note that each decision node contains a yes-or-no question. From each possible answer, one branch emanates, which can lead to another decision node or a leaf. This tree has a depth of 2, because the longest path from a leaf to the root goes through two branches.
A second (maybe not as good) decision tree we could use to decide whether we want to wear a jacket on a given day
A simple decision tree (decision stump) that consists of only the question, “Is it raining?” If the answer is yes, the decision we make is to wear a jacket. If the answer is no, the decision we make is not to wear a jacket.
A slightly more complicated decision tree than the one in figure 9.4, where we have picked one leaf and split it into two further leaves. This is the same tree as in figure 9.1.
The three apps we are recommending: Atom Count, an app for counting the number of atoms in your body; Beehive Finder, an app for locating the nearest beehives to your location; and Check Mate Mate, an app for finding Australian chess players in your area
If we split our users by platform, we get this split: the iPhone users are on the left, and the Android users on the right. Of the iPhone users, one downloaded Atom Count and two downloaded Check Mate Mate. Of the Android users, two downloaded Atom Count and one downloaded Beehive Finder.
If we split our users by age, we get this split: the young are on the left, and the adults on the right. Out of the young users, all three downloaded Atom Count. Out of the adult users, one downloaded Beehive Finder and two downloaded Check Mate Mate.
Classifier 1 uses platform, and classifier 2 uses age. To make the prediction at each leaf, each classifier picks the most common label among the samples in that leaf. Classifier 1 is correct four out of six times, and classifier 2 is correct five out of six times. Therefore, based on accuracy, classifier 2 is better.
Summary of the calculation of the Gini impurity index
The calculation of the Gini index for the set with eight black balls and two white balls. Note that if the total area of the square is 1, the probability of picking two black balls is 0.82, and the probability of picking two white balls is 0.22 (these two are represented by the shaded squares). Thus, the probability of picking two balls of a different color is the remaining area, which is 1 – 0.82 – 0.22 =0.32. That is the Gini index.
The two ways to split the dataset, by platform and age, and their Gini index calculations. Notice that splitting the dataset by age gives us two smaller datasets with a lower average Gini index. Therefore, we choose to split the dataset by age.
The two ways to split the dataset, by platform and age, and their entropy calculations. Notice that splitting the dataset by age gives us two smaller datasets with a lower average entropy. Therefore, we again choose to split the dataset by age.
A split of a dataset of size eight into two datasets of sizes six and two. To calculate the average Gini index and the average entropy, we weight the index of the left dataset by 6/8 and that of the right dataset by 2/8. This results in a weighted Gini index of 0.333 and a weighted entropy of 0.689.
When we split our dataset by age, we get two datasets. The one on the left has three users who downloaded Atom Count, and the one on the right has one user who downloaded Beehive Count and two who downloaded Check Mate Mate.
We can split the right leaf of the tree in figure 9.15 using platform and obtain two pure datasets. Each one of them has an accuracy of 100% and a Gini index and entropy of 0.
The resulting decision tree has two nodes and three leaves. This tree predicts every point in the original dataset correctly.
When we have a nonbinary feature, for example, one with three or more possible categories, we instead turn it into several binary (yes-or-no) features, one for each category. For example, if the feature is a dog, the answers to the three questions “Is it a dog?,” “Is it a cat?,” and “Is it a bird?” are “yes,” “no,” and “no.”
A graphic of the seven possible ways to split the users by age. Note that it doesn’t matter where we put the cutoffs, as long as they lie between consecutive ages (except for the first and last cutoff).
A dataset with two features (x0 and x1) and two labels (triangle and square) in which we will train a decision tree
If we have to use only one vertical or horizontal line to classify this dataset in the best possible way, which one would we use? Based on accuracy, the best classifier is the vertical line at x0 = 5, where we classify everything to the right of it as a triangle, and everything to the left of it as a square. This simple classifier classifies 8 out of the 10 points correctly, for an accuracy of 0.8.
The classifier in figure 9.21 leaves us with two datasets, one at each side of the vertical line. If we had to classify each one of them, again using one vertical or horizontal line, which one would we choose? The best choices are horizontal lines at x1 = 8 and x1 = 2.5, as the figure shows.
The resulting decision tree of depth 2 that corresponds to the boundary in figure 9.22. It has three nodes and four leaves.
A decision tree of depth 3 trained in the student admissions dataset
The plot of the dataset in table 9.6, where the horizontal axis corresponds to the age of the user and the vertical axis to the number of days per week that they engaged with the app
Left: The resulting decision tree obtained in Scikit-Learn. This tree has three decision nodes and four leaves. Right: The plot of the predictions made by this decision tree. Note that the cutoffs are at ages 35, 15, and 65, corresponding to the decision nodes in the tree. The predictions are 7, 6, 1.33, and 4.5, corresponding to the leaves in the tree.

FAQ

What is a decision tree and how is it structured?A decision tree is a model that predicts by asking a sequence of yes/no questions and following branches to a final decision. It consists of a root node (first question), decision nodes (internal questions), and leaf nodes (final predictions). Edges (branches) represent answers. Depth is the number of levels from the root to the deepest leaf. The simplest tree (a decision stump) has one question and two leaves.
How does a decision tree choose the “best” question to split the data?It evaluates each candidate split and picks the one that most cleanly separates the labels. Common criteria are:
  • Accuracy: fraction correctly labeled if you predict the majority class in each leaf (simple but coarse).
  • Gini impurity: lower is better; prefers purer leaves.
  • Entropy: lower is better; equivalent to maximizing information gain.
For fair comparison, impurity across leaves is combined with a weighted average (weights proportional to leaf sizes).
What is Gini impurity and why does it work?Gini impurity measures how mixed the classes are in a set. It can be interpreted as the probability that two randomly selected items (with replacement) from the set belong to different classes. A pure set has Gini = 0; more mixed sets have higher Gini (up to near 1 for many evenly mixed classes). When building trees, we prefer splits that minimize the weighted Gini of the resulting leaves.
What is entropy and information gain in decision trees?Entropy quantifies label uncertainty: 0 for a pure set, larger when classes are evenly mixed (e.g., 1 bit for a 50/50 binary mix). A split’s usefulness is often measured by information gain: the parent’s entropy minus the weighted average entropy of the children. Maximizing gain (equivalently, minimizing weighted entropy) yields purer leaves.
How do decision trees handle categorical features with more than two categories?They convert a multi-class categorical feature into several binary questions (one-hot encoding). For example, “animal ∈ {dog, cat, bird}” becomes “is dog?”, “is cat?”, “is bird?”. The tree evaluates each binary split with the chosen criterion (e.g., Gini/entropy) and uses the best.
How do decision trees split on numerical (continuous) features?They consider threshold questions like “feature ≤ t?”. Candidates are typically midpoints between sorted unique values. For each threshold, the tree computes the weighted impurity (or entropy) of the two resulting groups and selects the threshold that minimizes it (or maximizes information gain).
Why don’t we search all possible trees? Is the tree we get guaranteed to be optimal?Exhaustively searching all trees is computationally infeasible (the space explodes combinatorially). Practical algorithms grow trees greedily: at each node they choose the locally best split. This is fast and often works well, but it does not guarantee a globally optimal tree.
How do we prevent overfitting when growing a tree? What stopping rules and hyperparameters help?Stop splitting when no meaningful improvement is possible or when nodes are too small/deep. Common controls (Scikit-learn names in parentheses) include:
  • Maximum depth (max_depth)
  • Minimum samples to split a node (min_samples_split)
  • Minimum samples per leaf (min_samples_leaf)
  • Minimum impurity decrease to allow a split (min_impurity_decrease)
  • Limit features tried per split (max_features)
These “pre-pruning” settings help curb overfitting and improve generalization.
How do I build and use a decision tree in scikit-learn?
  • Prepare X (features) and y (labels).
  • Classification: use DecisionTreeClassifier(criterion="gini" or "entropy", plus hyperparameters); Regression: use DecisionTreeRegressor(criterion="squared_error" by default).
  • Fit: model.fit(X, y).
  • Predict: model.predict(X_new).
  • Control complexity with hyperparameters like max_depth, min_samples_split, min_samples_leaf, etc.
You can also visualize the tree to interpret splits and feature importance.
How do decision trees perform regression and what do their predictions look like?Regression trees split the feature space exactly like classification trees but choose splits by minimizing mean squared error (MSE). Each leaf predicts a constant—the average target value of training samples in that leaf—so the model is piecewise constant. In 2D, the decision boundary looks like a set of axis-aligned rectangles; in 1D, a step function across thresholds.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking Machine Learning, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking Machine Learning, Second Edition ebook for free