ML3: Classic models with scikit-learn

Lesson 2 of 5 ยท reading 15โ€“25 minutes; practice and project time is on the module page

Decision trees

๐ŸŽฏYou'll be able to

Train a decision tree, read its rules, and explain how depth controls what it can learn.

๐Ÿ The everyday picture

A doctor's checklist: "Fever? If yes, is it above 102? If yes, any rash?" Each answer leads to the next question. A decision tree learns such a checklist from data.

๐Ÿ“˜What it really is

๐Ÿ“A worked example

Input: decision trees of different depths on the kit (reference measurements, 2 November 2026, random_state=0):

Depth Training PR-AUC Validation PR-AUC (May)
2 0.4671 0.4184
4 0.597 0.4962
8 0.7937 0.3976
No limit 1.0 0.2307

The depth-2 tree's first question is "address score at most 0.33?", then "at most 0.20?". With no limit the tree memorises the training rows (1.0) and does worst on May. Depth 4 is the best of these.

Print a tree's rules with sklearn.tree.export_text(tree, feature_names=...).

๐ŸŒWhere it's used

๐Ÿ“Case scenario

A call centre trained a deep tree to route calls and was pleased with its training accuracy. A month later the routing looked random. The tree had learned rules like "calls at 10:42 from this exchange go to billing". Limiting depth and requiring at least 50 calls per leaf gave simpler rules that held up.

๐Ÿ–ฑ๏ธTry it

Train a depth-3 tree and print its rules. Success: you can read the three questions on the path to the riskiest leaf, and they make sense for parcels (address score, zone, slot or similar).

โš ๏ธCommon mistakes

๐Ÿ› ๏ธFor your project

Project 1's chosen model is made of trees. A shallow tree is a good way to explain to the hub team what drives the flags.

โœ…Quick check

What happens to a decision tree with no depth limit?

Show the answer

It memorises the training rows and does worse on new data.

What does a leaf predict?

Show the answer

The share of each class among the training rows that ended up in it.

โ† PreviousLinear and logistic regression
ProLearners Assist