← Back to the track

Learn machine learning · deep dive D1 · optional

Entropy versus Gini

Fourteen rows, four columns, and two ways to measure a mess. Compute the gains by hand under one measure, switch to the other, and say exactly what moved.

Deep dive D1DerivationAbout 40 minutesThe gate is a behaviour you produce

Optional, and it blocks nothing. It opens because 2.2 Impurity and splits is done. The gate is four computations within 0.002 plus one statement about what the criterion toggle changes.

01 · brief

Session 2.2 measured impurity with Gini and never said why that one. The usual alternative is entropy, and the usual claim is that the choice rarely matters. This is the table that claim is normally made on, so it is the table to check it on.

Entropy of a node is the sum over classes of minus p log2 p, measured in bits. Gini is one minus the sum of the squared proportions, which reads as the chance that two draws from the node disagree. Both are zero on a pure node and largest on an even mix. Both are used the same way: score every branch, weight by rows, subtract from the parent.

The question you cannot answer yet: does swapping the measure change which column the tree splits on first, and if not, what does it change?

02 · commit

Before anything runs

Before any arithmetic: switching from entropy to Gini on these fourteen rows does what to the ranking of the four columns?

Before any arithmetic: switching from entropy to Gini on these fourteen rows does what to the ranking of the four columns?

Choose something first.

03 · work

To three decimals or better, within 0.002.

Attempts: 0

To three decimals or better, within 0.002.

Attempts: 0

To three decimals or better, within 0.002.

Attempts: 0

To three decimals or better, within 0.002.

Attempts: 0

0 of 4 computations accepted.

The fourteen rows

computed in this browserpublic-domain teaching table
The fourteen-row weather table: four categorical columns and a play column.
rowoutlooktemperaturehumiditywindplay
1sunnyhothighweakno
2sunnyhothighstrongno
3overcasthothighweakyes
4rainmildhighweakyes
5raincoolnormalweakyes
6raincoolnormalstrongno
7overcastcoolnormalstrongyes
8sunnymildhighweakno
9sunnycoolnormalweakyes
10rainmildnormalweakyes
11sunnymildnormalstrongyes
12overcastmildhighstrongyes
13overcasthotnormalweakyes
14rainmildhighstrongno

Nine plays and five does not, so the parent counts are 9 and 5. This table is a public-domain teaching fixture. It is not drawn from the loan generator.

Which criterion the table below is measured with

Parent entropy: 0.940286.

Weighted impurity and gain for each column under entropy.
columnbranchesweightedgain
outlook30.6935360.246750
temperature30.9110630.029223
humidity20.7884500.151836
wind20.8921590.048127

Under entropy the parent scores 0.940286 and the four columns rank outlook 0.246750, humidity 0.151836, wind 0.048127, temperature 0.029223.

Show the same four rows as a table
Values shown in the figure above
columnbranchesweightedgain
outlook30.6935360.246750
temperature30.9110630.029223
humidity20.7884500.151836
wind20.8921590.048127
Show every branch
Per-branch counts, impurity and weight under entropy.
columnbranchplays and does notimpurityweight
outlooksunny2 and 30.9709510.3571
outlookovercast4 and 00.0000000.2857
outlookrain3 and 20.9709510.3571
temperaturehot2 and 21.0000000.2857
temperaturemild4 and 20.9182960.4286
temperaturecool3 and 10.8112780.2857
humidityhigh3 and 40.9852280.5000
humiditynormal6 and 10.5916730.5000
windweak6 and 20.8112780.5714
windstrong3 and 31.0000000.4286

The two rankings, side by side

Entropy
outlook, then humidity, then wind, then temperature
Gini
outlook, then humidity, then wind, then temperature

Same order, different units. Neither criterion is the more correct one, and this table says nothing about which performs better in general.

Ask for a hint

Rungs open as you run the session, not as time passes. You have run it 0 times.

1. A question: available after 3 evaluated runs.

2. The answer: available after 5 evaluated runs.

04 · verdict

Finish the four computations and switch the criterion at least once, and the verdict states both rankings against what you committed to.

05 · gate

Your check: an outcome you produce

not yet

All four entropy computations within 0.002, plus the correct statement of what the Gini toggle changes.

Not met yet. Keep working the session.

What the toggle changes on this table

Choose one first.

carry

Your takeaway

Edit it into your own words, or keep the line as it is.

back to the track

2.2 Impurity and splits

A deep dive blocks nothing. Open the notebook