0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09Twenty questions, as a model
You've probably played twenty questions. Is it an animal? Is it bigger than a cat? Each yes or no narrows things down, until you know the answer. A decision tree is a model that plays exactly that game. "Has this customer ordered in the last three weeks?" No. "Has it been more than five weeks?" Yes. "Then they're likely to leave." Meera loves it, because she can draw it on a whiteboard, and anyone can follow it. But how does a machine decide which question to ask first, and where to draw the line? The answer comes from a beautiful idea called information, measured in bits.
And we'll also see how a tree that keeps asking questions can end up memorising individual customers. Season One, Module Four, stop four: decision trees.
1:04Anatomy of a decision tree
A tree starts at the root, the first question. Each answer follows a branch to another question, until we reach a leaf, which holds the prediction. Here's the geometry behind it. Every question compares one feature with a threshold, like "days since last order less than twenty-two point five?" On a plot, that's a straight cut, parallel to an axis. Question by question, the tree slices the space into rectangles, and each rectangle gets a prediction. To predict for a new customer, just drop them in at the root and follow the answers down.
A customer at forty days goes "no", then "no", and lands in a leaf that says "leaves".
1:49Impurity: entropy and Gini
To choose good questions, we first need a way to measure how mixed up a group is. That's called impurity. Picture bags of marbles: red for customers who left, green for those who stayed. A bag of all green is pure. If you draw a marble, you know exactly what you'll get. A bag that's half red and half green is as uncertain as it gets. One way to measure this is entropy, from information theory. For each class, take its share p, multiply by the log base two of p, add them up, and flip the sign. A pure bag has an entropy of zero.
A fifty-fifty bag has an entropy of exactly one bit: the information in one fair coin flip. A bag with five reds and one green is in between, about zero point six five. Another popular measure is Gini impurity: one minus the sum of the squared shares. It's zero for a pure bag, zero point five for fifty-fifty, and it has a neat meaning: the chance that two marbles drawn at random have different colours. The two curves look very similar, both arches peaking at fifty-fifty, and in practice they usually pick very similar splits.
3:06Information gain
Now we can score a question. A good question should leave us with purer groups than we started with. At the root, Brightcart's ten customers are five-and-five, an entropy of one bit. Let's try the question "days since last order less than twenty-two point five?" Four customers answer yes, and all four stayed: entropy zero. Six answer no, and five of those left: entropy about zero point six five. We weight each group by its size: four tenths of zero, plus six tenths of zero point six five, gives zero point three nine.
We started at one bit and we're down to zero point three nine, so this question gained zero point six one bits of information. That's called the information gain. Now try "days less than ten?" Only two customers answer yes, and the other group of eight is still badly mixed. The information gain is just zero point two four. The first question wins easily. And with Gini impurity instead of entropy, it wins too.
4:14How the tree searches for splits
But how did the algorithm find twenty-two point five in the first place? By brute force, which is fine, because it's fast. It sorts the customers by days, and tries a threshold halfway between every pair of neighbouring values. For each one, it computes the information gain. Here's the chart: the best thresholds are twenty-two point five and thirty-six point five, which tie. If there are more features, it does the same scan over every feature, and picks the single best question overall. Then it repeats the whole process inside each child group, treating it as a fresh little dataset.
And again inside their children. This is recursion: the tree grows itself, one best question at a time. It stops when a group is pure, when a maximum depth is reached, when a group gets too small, or when no question helps enough. Notice that the tree is greedy. At each step it takes the best question right now, without thinking ahead. That makes it fast, though not always perfect.
5:27Regression trees
Trees aren't just for classification. A regression tree predicts numbers. Each leaf simply predicts the average of the training examples that land in it. For Brightcart's deliveries, a first split might ask "distance under five and a half kilometres?" Short trips average twenty minutes; long trips average about forty-two. Instead of entropy, splits are chosen to reduce the variance, the squared error, within each group. The result is a staircase rather than a smooth line, a quirk to remember: a single tree can never predict outside the range of values it saw in training.
6:08Overfitting and pruning
Now the danger. If we let the tree keep asking questions until every leaf is pure, it will. Look inside the right-hand group. There's one customer at thirty-three days who stayed, sitting between people who left. To make that leaf pure, the tree adds a split at thirty-six point five, and then another at twenty-nine. Read the final rule out loud: customers away twenty-three to twenty-nine days are leaving, twenty-nine to thirty-six days are staying, and over thirty-six are leaving again. That middle band makes no business sense at all.
It exists only to fit one customer. Training accuracy: one hundred percent. That's memorisation, the overfitting we met in episode six, in its purest form. The cure is pruning. Limit the maximum depth. Require at least a few customers in every leaf. Or grow the full tree and then cut back branches whose improvement doesn't justify their complexity, with the pruning strength chosen by cross-validation. A shallow tree that Meera can read is usually the more honest model too.
7:20Strengths, weaknesses and feature importance
Trees have real strengths. They're readable. They handle numbers and categories, with no scaling needed. They automatically capture interactions, like "long trips are only slow when it rains". And they tell us which features mattered most, by adding up how much impurity each feature removed. But they have weaknesses too. They're unstable. Remember how the splits at twenty-two point five and thirty-six point five tied? Remove a single customer and the root question can flip from one to the other, reshaping the whole tree.
Their axis-aligned cuts struggle with diagonal boundaries, building clumsy staircases instead. And a single tree overfits easily. Here's the twist. Those weaknesses, especially the instability, turn out to be the key to one of the most powerful ideas in machine learning: combining many trees into a forest. That's next.
8:20Try it yourself: score a split
Let's score a split ourselves: "days since last order less than thirty-six and a half?" Six customers answer yes, and just one of them left. Four answer no, and all four left. Work out the weighted Gini impurity. Pause now.
The yes-group has one leaver in six: one minus a sixth squared, minus five sixths squared, about zero point two eight. The no-group is pure: zero. Weighted by size: six tenths of zero point two eight, plus nothing, which is about zero point one seven. With entropy, the information gain is zero point six one, exactly the same as the split at twenty-two and a half. Two different questions, equally good on this data. Remember that tie: it's at the heart of why trees are unstable.
9:19Try it yourself: a regression-tree split
Regression trees choose splits by how much they reduce squared error. Before any split, Brightcart's eight deliveries vary around their average of twenty-eight point four minutes, with a total squared error of about thirteen hundred. Split at five kilometres: the short trips average twenty minutes and the long trips about forty-two. Measured around each group's own average, the squared error drops to about three hundred and sixty-seven, a reduction of almost seventy-two percent with a single question. At every node, a regression tree searches for the question that reduces this error the most.
Keep this picture in mind for next episode, because gradient boosting is built from exactly these small regression trees.
10:06More than two classes
Trees handle many classes without any change in logic. For a node with three equally common ticket types, the Gini impurity is one minus three times a ninth, about zero point six seven, and the entropy is log base two of three, about one point five nine bits, more than one bit, because there's more uncertainty with three options than with two. A pure node is still zero. Each leaf predicts its most common class, or reports the class proportions as probabilities.
10:38Missing values and categories
Trees are flexible with messy data. Some implementations can split directly on categories, asking whether the payment method is card or wallet versus cash, without one-hot encoding. And for missing values, there are several options depending on the software: fill them in beforehand, send missing values down whichever branch works best, or use a backup question on a related feature. Capabilities differ between libraries, so check what yours supports.
11:07Reading feature importance carefully
Feature importance from trees is useful, but easy to misread. It tends to favour features with many distinct values, so an almost-unique column, like an order number, can look important just because the tree used it to memorise. When two features are near-copies, they share the credit, so each looks less important than it really is. And importance never proves cause and effect. A more reliable check is permutation importance: shuffle one feature's values in the validation data, breaking its link with the answer, and measure how much the accuracy drops.
A big drop means the model genuinely relies on it.
11:48Where single trees shine
So where do single trees shine? Wherever decisions must be explained in plain rules: if a parcel weighs over twenty kilograms and the address has no lift, schedule a two-person delivery. In quick prototypes. In settings where every decision must be justified. And, most importantly, as the building block of the forests and boosted ensembles that dominate table-shaped data, which is exactly where we're going next.
12:19Myth vs Fact
Myth: a deeper tree is always more accurate. Fact: deeper trees are always more accurate on the training data. On new data, accuracy usually peaks at a modest depth and then falls, exactly like the complexity curve from episode six.
12:36Recap
Let's recap. A decision tree asks a sequence of yes-or-no questions, slicing the data into rectangles. Entropy and Gini measure how mixed a group is. The tree picks the question with the largest information gain, searching every feature and threshold, and repeats inside each branch. Regression trees predict averages. And because trees easily memorise, we prune them. One sentence to remember: ask the question that removes the most uncertainty, but stop before you memorise.
13:11Next: random forests and boosting
Mission complete. Meera's flowchart has one powerful question: has this customer ordered in the last twenty-two days? If not, they're at risk. Simple, readable, and backed by information theory. But one tree is wobbly. So what happens if we grow a hundred different trees and let them vote? Or train trees one after another, each fixing the last one's mistakes? Those two ideas, random forests and gradient boosting, are among the most successful algorithms ever made for table-shaped data. That's next. See you on the map.