Lessons › Chapter 6: Trees and ensembles
Entropy and Gini
A tree wants groups that are as “pure” as possible: all one answer. impurity measures how mixed a group is. All teal: impurity 0. Half and half: as mixed as it gets.
Watch it move. This lesson is interactive on Gradient Lab: every step is an animated scene you can replay.
Start the interactive lessonStep by step
Which question first?
Building the tree, Leo has to choose which question to ask first.
How mixed is a group?
A tree wants groups that are as “pure” as possible: all one answer. impurity measures how mixed a group is. All teal: impurity 0. Half and half: as mixed as it gets.
Try a question
Start with 5 passes and 5 fails, then try a question that splits them. This split leaves both sides still mixed: it didn’t help much.
Try another question
This question puts almost all passes on one side and all fails on the other. Both sides have low impurity.
Pick the purest split
The tree tries every possible question and keeps the one with the lowest impurity afterwards. entropy and gini are two popular ways to score impurity; they usually pick the same question.
In real life: sorting laundry
When you sort laundry, you ask the question that makes the cleanest piles first. “White or colored?” beats “cotton or not?” because it leaves piles that won’t ruin each other. Trees pick questions the same way, by measuring purity.
Quick check: Which group has the lowest impurity?
- 5 cats and 5 dogs
- 10 cats (correct)
- 6 cats and 4 dogs
A group with only one class is perfectly pure: impurity 0.
Pseudocode
impurity of a group = how mixed it is
pure group (all one class) → impurity 0
50 / 50 mix → impurity highest
for each possible question:
split the group, measure the impurity of each side
pick the question that leaves the purest groups
# entropy and Gini are two ways to measure impurity
Key terms
- impurity
- How mixed a group is. A group of only one class has impurity 0.
- entropy
- One way to measure impurity, borrowed from information theory.
- gini
- Another common measure of impurity: the chance two random picks from the group disagree.
Build it: Loan decisions with trees and forests (35 min, runs in your browser)
See the project