Read a finished tree
At checkout we know two features – distance and weather. After delivery we learn the label – late or on time. Each training order pairs its features with its known label.
- Root · Is the distance ≤ 2.5 km?
- Yes → Predict on time · A, B · 0 of 2 late
- No → Is it raining?
- Yes → Predict late · D, F, H · 3 of 3 late
- No → Predict on time · C, E, G · 1 of 3 late
The root is the first question. A branch is a possible answer. A leaf is where we stop and predict using the training orders that reached it.
F was a 4 km delivery in rain. Which two answers lead to its prediction?
Reveal the path
No at the distance question, then Yes at the rain question. All three orders in this leaf were late, so it predicts late.
See the eight labeled training orders
| Order | Distance | Weather | Outcome |
|---|---|---|---|
| A | 2 km | Dry | On time |
| B | 2 km | Rain | On time |
| C | 3 km | Dry | On time |
| D | 3 km | Rain | Late |
| E | 4 km | Dry | Late |
| F | 4 km | Rain | Late |
| G | 5 km | Dry | On time |
| H | 5 km | Rain | Late |
What does Gini count?
Start with four training orders. A, B and C were on time. D was late. Pick a card, put it back, then pick again. A pair counts as different when one is late and the other is on time.
Pick A (on time), put it back, then pick D (late). Their outcomes differ. A followed by B gives the same outcome. You may draw the same card twice.
Pick the first card
Count all 16 equally likely pairs
| First ↓ / Second → | A | B | C | D |
|---|---|---|---|---|
| A | Same | Same | Same | Different |
| B | Same | Same | Same | Different |
| C | Same | Same | Same | Different |
| D | Different | Different | Different | Same |
Each row is a first pick. Each column is a second pick. Six cells contain different labels.
Gini = 6/16 = 0.375
With two labels, let p be the late fraction. The two ways to differ are late then on time, or on time then late. Their probabilities add to G = 2p(1 − p). Gini runs from 0 (all labels alike) to 0.5 (equal counts).
Return to all eight training orders. Four were late and four were on time, so G = 2 × (4/8) × (4/8) = 0.500. This is the starting point for every candidate root question.
A question divides the orders at this node into Yes and No groups. Open any candidate to see its counts and calculations. Weight each child’s Gini by its share of the orders. The question with the largest drop wins.
Compare the root questions
Before the split · A, B, C, D, E, F, G, H · 4 late, 4 on time. Gini = 2 × (4/8) × (4/8) = 1/2.
Distance ≤ 2.5 km? · drop 0.167
Yes · A, B · 0 late, 2 on time. Gini = 2 × (0/2) × (2/2) = 0.
No · C, D, E, F, G, H · 4 late, 2 on time. Gini = 2 × (4/6) × (2/6) = 4/9.
Weighted Gini = (2/8) × 0 + (6/8) × 4/9 = 1/3 ≈ 0.333.
Drop = 1/2 − 1/3 = 1/6 ≈ 0.167.
Distance ≤ 3.5 km? · drop 0.125
Yes · A, B, C, D · 1 late, 3 on time. Gini = 2 × (1/4) × (3/4) = 3/8.
No · E, F, G, H · 3 late, 1 on time. Gini = 2 × (3/4) × (1/4) = 3/8.
Weighted Gini = (4/8) × 3/8 + (4/8) × 3/8 = 3/8 ≈ 0.375.
Drop = 1/2 − 3/8 = 1/8 ≈ 0.125.
Distance ≤ 4.5 km? · drop 0.000
Yes · A, B, C, D, E, F · 3 late, 3 on time. Gini = 2 × (3/6) × (3/6) = 1/2.
No · G, H · 1 late, 1 on time. Gini = 2 × (1/2) × (1/2) = 1/2.
Weighted Gini = (6/8) × 1/2 + (2/8) × 1/2 = 1/2 ≈ 0.500.
Drop = 1/2 − 1/2 = 0 ≈ 0.000.
Rain at checkout? · drop 0.125
Yes · B, D, F, H · 3 late, 1 on time. Gini = 2 × (3/4) × (1/4) = 3/8.
No · A, C, E, G · 1 late, 3 on time. Gini = 2 × (1/4) × (3/4) = 3/8.
Weighted Gini = (4/8) × 3/8 + (4/8) × 3/8 = 3/8 ≈ 0.375.
Drop = 1/2 − 3/8 = 1/8 ≈ 0.125.
Choose distance ≤ 2.5 km. Both short trips were on time. Six longer trips go to the next node.
Compare questions for the six longer trips
Before the split · C, D, E, F, G, H · 4 late, 2 on time. Gini = 2 × (4/6) × (2/6) = 4/9.
Distance ≤ 3.5 km? · drop 0.028
Yes · C, D · 1 late, 1 on time. Gini = 2 × (1/2) × (1/2) = 1/2.
No · E, F, G, H · 3 late, 1 on time. Gini = 2 × (3/4) × (1/4) = 3/8.
Weighted Gini = (2/6) × 1/2 + (4/6) × 3/8 = 5/12 ≈ 0.417.
Drop = 4/9 − 5/12 = 1/36 ≈ 0.028.
Distance ≤ 4.5 km? · drop 0.028
Yes · C, D, E, F · 3 late, 1 on time. Gini = 2 × (3/4) × (1/4) = 3/8.
No · G, H · 1 late, 1 on time. Gini = 2 × (1/2) × (1/2) = 1/2.
Weighted Gini = (4/6) × 3/8 + (2/6) × 1/2 = 5/12 ≈ 0.417.
Drop = 4/9 − 5/12 = 1/36 ≈ 0.028.
Rain at checkout? · drop 0.222
Yes · D, F, H · 3 late, 0 on time. Gini = 2 × (3/3) × (0/3) = 0.
No · C, E, G · 1 late, 2 on time. Gini = 2 × (1/3) × (2/3) = 4/9.
Weighted Gini = (3/6) × 0 + (3/6) × 4/9 = 2/9 ≈ 0.222.
Drop = 4/9 − 2/9 = 2/9 ≈ 0.222.
Choose rain. All three rainy trips were late. Two of three dry trips were on time.
Build the delivery tree
Training orders · distance, weather and outcome
| Order | Distance | Weather | Outcome |
|---|
Predict an unseen delivery
Choose a delivery or enter its features. Compare a tree that stops after distance with one that can also ask about rain. The app warns when the estimated chance of delay is at least 50%.
P · 4.2 km in rain
One question gives 4/6 late. Two questions give 3/3 late. Both trees warn.
Q · 3.2 km and dry
One question gives 4/6 late and warns. Two questions give 1/3 late and stay silent.
Check your understanding
At exactly 2.5 km in rain, which leaf do we reach?
The ≤ sign includes exactly 2.5. We take Yes and stop with A and B. Both arrived on time, so the estimate is 0/2 late.
Why did distance win at the root and rain win next?
The root compares questions using all eight orders. The next node compares questions using only the six longer trips. Rain has the largest Gini drop within those six.
Does 3 out of 3 late guarantee the next rainy delivery will be late?
No. The fraction describes the three training orders in this leaf. Check predictions on unseen deliveries to learn how well the rule generalizes.
Teacher prompt · Why stop at the mixed dry leaf?
This lesson limits each path to two questions. Two of the three dry, longer orders were on time, so the leaf predicts on time. A deeper tree could split again. In practice, choose depth using validation data.