Gradient LabStart the lesson

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 lesson

Step by step

Story

Which question first?

Building the tree, Leo has to choose which question to ask first.

Concept

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.

Build

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.

Build

Try another question

This question puts almost all passes on one side and all fails on the other. Both sides have low impurity.

Key idea

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.

Real life

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?

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