Let me first explain the concept of entropy for decision trees:
Entropy is a so called impurity measure for a set of elements. Impurity - being the opposite of purity - is referring to the distribution of the decision categories (or class labels) within the set. Initially, each row in the table is one element and your set of elements are all rows of your table. This set is called pure if it just consists of the same class label and it is called impure if it contains all different class labels in the same proportion. Translating into the values of entropy (remember being an impurity measure, the set has in the latter case the highest possible value and in the former case (just the same class label) the lowest value. The lowest possible value for entropy is 0.
So let's choose a visual respresentation to illustrate what really counts and what does not matter for the entropy.
1) You start off with the initial table which contains the following class labels:
Please note that for computing the entropy none of the remaining attributes, attribute values matter, also the order of the rows don't matter (entropy is a measure for a set of elements)
2) You convert the different class labels to different colors
3) So instead of this table with the colored class labeled, you just draw a circle ("ball") for each class label (=element)
Now you throw all the balls in a bag. There you go! Your elements of a set are represented as balls in a bag. If you open the bag and look into it, you might see
- just balls of the same color --> one pure color. The entropy says, it's not impure at all, I give it a zero! entropy = 0
- the same amount of balls in different colors --> that doesn't look pure at all, it's impure! The entropy assigns the highest possible value to this set (bag). The highest possible entropy value depends on the number of class labels. If you have just two, the maximum entropy is 1. In your case, you have 4 different class labels, so the maximum entropy would be e.g. if you had 12 balls and 3 balls of each color. The maximum entropy of a 4 class set is 2.
- none of the above. Then your entropy is between the two values. If one color is dominant then the entropy will be close to 0, if the colors are very mixed up, then it is close to the maximum (2 in your case).
How does a decision tree use the entropy?
Well, first you calculate the entropy of the whole set. That impurity is your reference. What your decision tree tries to achieve is to reduce the impurity of the whole set. So the information Gain for a given attribute is computed by taking the entropy of the whole set and subtracting it with the entropies of sets that are obtained by breaking the whole set into one piece per attribute category. To make sure a set with just a few elements don't get the same weight as a set with many elements the entropies of sets are multiplied by their relative frequency.
Coming back to the example, for computing the Gain (S, parents) you need to compute the weighted entropies of the two sets: set 1 = all rows with category(parent) = yes and set 2 = all rows with category(parent) = no).
Set 1 refers to five rows, all having the decision category = cinema(just 5 orange balls!)
So the weight is 5 rows out of 10 rows = 5/10 = 0.5. And the entropy is 0 (totally pure) or by applying the formula:
You have a sum over all class labels so n = 4. So you assign a class label to each of the i's. E.g.
For i = 1 being cinema: - (5/5 * log(3/3) = - (1 * log(1)) = - (1*0) = 0
For i = 2 being tennis: - (0/5 * log(0/3) = - (0 * log(0)) = 0
For i = 3 being stay in: - (0/5 * log(0/3) = - (0 * log(0)) = 0
For i = 4 being shopping: - (0/5 * log(0/3) = - (0 * log(0)) = 0
So the sum of all four is 0. 0 times the weight 0.5 equals 0.
Set 2 also refers to five rows, having the decision categories = 1*cinema, 2*tennis, 1*stay in, 1*shopping
So the weight is also 5 rows out of 10 rows = 5/10 = 0.5. Applying the formula:
For i = 1 being cinema: - (1/5 * log(1/5) = - (0.2 * -2,32) = 0.46
For i = 2 being tennis: - (2/5 * log(2/5) = - (0.4 * -1,32) = 0.53
For i = 3 being stay in: - (1/5 * log(1/5) = - (0.2 * -2,32) = 0.46
For i = 4 being shopping: - (1/5 * log(1/5) = - (0.2 * -2,32) = 0.46
So the sum of all four is 1.92. 1.92 times the weight 0.5 = 0.96.
Thus, the Gain for this attribute is:
Initial entropy - weighted entropies for all attribute categories:
1.57 - 0 - 0.96 = 0.61.
For the other attributes you do the same calculation.