Build garageTry it → read → next · ~10 min

Tutorials · Chapter D (4/4) · ~10 min

Decision trees

Try it → see it → read → next

Build a branching predictor whose path you can trace from question to answer.

Try yourself

Playground

Choose the first split

Pick the rule a decision tree should use at a branch.

Recap

What you just did

You made a prediction by following feature splits. Each branch narrowed the remaining examples; the leaf returned a class or number. Unlike a wall of weights, the path can be read aloud—though that does not guarantee it is correct or fair.

Teach

How it works

Here is a hand-built classification tree:

def predict(hours, practice_tests):
    if hours >= 4:
        if practice_tests >= 2:
            return "ready"
        return "almost"
    return "needs-practice"

print(predict(hours=5, practice_tests=3))

Training searches for splits that make the resulting groups purer or reduce prediction error.

  1. Root asks the first feature question
  2. Branch follows the answer
  3. Node asks another question if needed
  4. Leaf returns the prediction

Mental model: a tree is a choose-your-own-adventure where each answer narrows the outcome.

Use it

When you'd use this

  • Building an interpretable baseline for tabular data
  • Mixing numeric and category-like features
  • Explaining why one row received a prediction

Watch out

Watch out

A deep tree can memorize training rows and become brittle. Limit depth, require enough examples per leaf, and evaluate on held-out data. A readable discriminatory rule is still discriminatory—inspect sensitive proxies.

Try next

Try this next

Change the first threshold from 4 to 6. List which example types would now travel down a different branch.