Decision TreesInformation Gain
A cut is good when the boxes after it are much purer than the box before it. The drop in entropy is called the information gain. The two new boxes can be of different sizes, so we average their entropies with weights:
Let's score Meena's first question, "length ≤ 10.25 cm", by hand.
- Before: 10 Alphonso and 10 Totapuri, an even mix, so
. - Yes side: 9 Alphonso and 1 Totapuri. With and , we get
. - No side: 1 Alphonso and 9 Totapuri — the same mix the other way round, so too.
Each side holds 10 of the 20 mangoes, so , and the gain is
Now the best question about weight, "weight ≤ 230 g". Its yes side holds 7 Alphonso and no Totapuri, so . Its no side holds 3 Alphonso and 10 Totapuri, , and it has 13 of the 20 mangoes. So its gain is . Which question should the tree ask first? The one about
Length wins, 0.53 against 0.49. The tree always asks the question with the largest information gain first.
Try Every Cut
The computer does not guess. It slides the cut through every gap between two neighbouring mangoes, for every feature, and works out the gain each time. Slide the cut and watch the gain chart fill in.
That scan is exactly how the computer grows a tree:
- Work out the entropy of the box.
- Try every feature, and every cut halfway between two neighbouring values.
- For each cut, work out the weighted entropy of the two new boxes, and so the information gain.
- Ask the question with the largest gain, then repeat all four steps inside each new box.
Note
Entropy is not the only way to measure a mess. Gini impurity is 1 minus the square of each kind's share; for 9 Alphonso and 1 Totapuri it is . Like entropy, it is 0 for a pure box and largest for an even mix, but it needs no logarithms. The two usually choose the same questions; scikit-learn uses Gini unless you ask for entropy.