Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Machine Learning Foundations — Alpha School

Lesson 8: Decision Trees and Random Forests

Some of the most useful models in machine learning work the way people naturally make decisions: by asking a series of yes-or-no questions. Is the customer under 30? If yes, do they shop online? If no, have they bought from us before? A decision tree is exactly that — a flowchart of questions that splits the data into smaller and smaller groups until each group is mostly one kind of answer. It is the most human-readable model in this course, and it can handle both numbers and categories without any special preparation.

Building a tree means choosing, at each step, the question that best separates the data. Imagine a worked example: a small bank wants to predict whether a loan applicant will repay. The data includes income, age, and whether the applicant has defaulted before. The tree might first ask whether the applicant has defaulted before, because that single question separates the risky applicants from the safe ones better than any other. Then, among those who have not defaulted, it might ask about income, and so on. Each split is chosen to make the resulting groups as pure as possible — groups where nearly everyone has the same label. The tree keeps splitting until the groups are pure enough, or until a limit is reached.

Trees that memorise, and forests that fix it

A single decision tree has a weakness: if you let it grow without limits, it will keep asking questions until every group contains just one or two examples, and it will have memorised the training data — including its noise. That is overfitting, and you will study it properly in the next lesson. The standard fix is to grow many trees instead of one, and let them vote. That is a random forest. Each tree in the forest is trained on a different random sample of the data, and at each split it is only allowed to consider a random subset of the features. The trees therefore make different mistakes, and when they vote together, the mistakes cancel out while the real pattern survives.

Random forests are popular because they work well with very little tuning, handle messy real data, and still tell you something useful: which features mattered most. If income appears in splits all over the forest while age rarely does, income is the more important feature. That said, a forest of hundreds of trees is no longer something a person can read like a flowchart — you trade the single tree's transparency for the forest's accuracy. For many real projects that is a fair trade, and it is why random forests remain a first choice for tabular data: the neat rows and columns of spreadsheets and databases.

Keeping trees honest: pruning

A tree that grows without limits will memorise its training data, so the practical question is how to stop it in time. The simplest answer is to set limits before training: a maximum depth, so no path can ask more than a handful of questions; a minimum number of examples in each group, so the tree cannot split down to single examples; or a minimum improvement, so a split must make the groups meaningfully purer before it is allowed. A more careful answer is pruning: grow the full tree, then cut back the branches that do not improve the score on held-out data, working from the leaves upwards. Either way, the limit is chosen by cross-validation, not by guessing — you try several depths, measure each on held-out folds, and keep the one that generalises best. The same discipline applies to random forests: more trees help up to a point, and after that they only add computing time, so the number of trees is also a choice to be tested rather than assumed.

Trees also have a quiet advantage over the models you met earlier: they do not care about the scale of your features, and they can capture relationships that are not straight lines. A tree can learn that income matters only for young applicants, or that a feature helps up to a point and then stops — patterns a straight line cannot express. The price is that trees draw hard, jagged boundaries between groups, which can look crude beside the smooth curves of other models. As always in machine learning, there is no best model, only the best model for the data and the decision in front of you.

Watch & Learn

One video to watch for this unit, from a trusted educator.

  • Linear Regression, Clearly Explained!!! — StatQuest with Josh Starmer
    Josh Starmer explains fitting a line with least squares and R-squared, with worked numbers throughout — the companion to Lessons 5 and 6.

Key Terms

Decision tree
A model that predicts by asking a series of yes-or-no questions, splitting the data into smaller groups at each step.
Split
One question in a decision tree that divides the examples into two groups.
Purity
How much the examples in a group share the same label; a split is chosen to make the groups as pure as possible.
Random forest
Many decision trees trained on different random samples, whose votes are combined into one prediction.
Feature importance
A measure of how much each feature contributed to a model's predictions, reported by models such as random forests.
Tabular data
Data in neat rows and columns, like a spreadsheet, where each row is an example and each column is a feature.
Ensemble
A model built by combining many simpler models, such as the trees in a random forest.

Discussion Questions

  1. A single tree can be drawn and read by a person, but a forest of 500 trees cannot. When would you choose readability over accuracy?
  2. Why does letting each tree see only a random subset of features make the forest better, not worse?
  3. A bank uses a tree to decide loans. What questions would you worry about if the tree's first split was the applicant's postcode?

Practice Questions

  1. How does a decision tree make a prediction?
  2. What makes one split better than another when building a tree?
  3. Why does a single deep tree tend to overfit?
  4. How does a random forest fix that?
  5. What is feature importance, and why is it useful?
Show answers
  1. By following a series of yes-or-no questions down to a group, then predicting that group's most common label. Each question splits the data, and the final group gives the answer.
  2. How pure the resulting groups are. A good split produces groups where nearly everyone shares the same label.
  3. Because it keeps splitting until groups are tiny, memorising the training data including its noise. A tree with no limits can fit every quirk of the training examples and fail on new ones.
  4. It trains many trees on different random samples and lets them vote. The trees make different mistakes, so voting cancels the mistakes out while the real pattern survives.
  5. It shows how much each feature contributed to the model's predictions. It tells you which measurements actually mattered, such as income mattering more than age for loan repayment.
Keep reading to complete this lesson