../ Data Science
-rw-r--r--aguang9.0 KiB8 min read

modified

Entropy and Information Gain

1. Play golf or not?

To begin with this topic, let's think about a question from the dataset given below.

OutlookTemperatureHumidityWindyPlay Golf
sunnywarmlownoyes
sunnyhotmediumnono
rainywarmlowyesno
overcasthothighnono
sunnywarmmediumyesyes
rainyhotmediumyesno
sunnywarmlownoyes
overcastwarmlowyesyes
rainywarmhighnono
overcasthotmediumyesyes

From the dataset of people playing golf or not in different conditions, which attribute do you think is most useful to predict wheather people play golf or not?

2. What is entropy?

To answer this question, one simple idea could be choosing the attribute of which the distribution is most complicated from the given dataset.

But, how to quantify the complexity of an attribute?

Here, entropy could be useful.

2.1 The definition of entropy.

Entropy is a concept from information theory, which is used to measure the amount of uncertainty in a system. It measures the average number of bits required to represent the state of a system.

H(X)=−∑xP(X=x)log⁡2P(X=x)H(X) = -\sum_{x} P(X = x) \log_{2} P(X = x)

We define:

0log⁡20=00 \log_{2} 0 = 0

Basically, higher entropy of a system indicates more information that the system has. Or, in other words, the system is more complicated.

2.2 The entropy of Play.

There are 5 yes instances and 5 no instances of attribute Play in this dataset.

P(Play=yes)=510=12P(Play = yes) = \frac{5}{10} = \frac{1}{2} P(Play=no)=510=12P(Play = no) = \frac{5}{10} = \frac{1}{2}

The entropy of class attribute Play is:

H(Play)=−12log⁡212−12log⁡212=1H(Play) = - \frac{1}{2} \log_{2} \frac{1}{2} - \frac{1}{2} \log_{2} \frac{1}{2} = 1

2.3 The entropy of Temperature.

There are 6 warm instances and 4 hot instances of attribute Temperature in this dataset.

P(Temperature=warm)=610=35P(Temperature = warm) = \frac{6}{10} = \frac{3}{5} P(Temperature=hot)=410=25P(Temperature = hot) = \frac{4}{10} = \frac{2}{5}

The entropy of attribute Temperature is:

H(Temperature)=−35log⁡235−25log⁡225≈0.97H(Temperature) = - \frac{3}{5} \log_{2} \frac{3}{5} - \frac{2}{5} \log_{2} \frac{2}{5} \approx 0.97

2.4 The answer through compraing entropy.

Similarly, we can caculate the entropies of each attribute:

AttributeEntropy
Outlook1.57
Temperature0.97
Humidity1.52
Windy1

By considering the entropies of different attributes, Outlook could be most useful to predict whether people play golf.

3. What is information gain?

So far, you've got some kind of answer for the question given in section 1.

But, as you might already concerned, what we really care is the complexity of relation between specific attribute and class attribute instead of the compexity of the attribute itself.

Now, you need information gain.

3.1 The definition of information gain.

Basically, information gain measures how much does the relation between X and Y bring.

IG(Y,X)=H(Y)−H(Y∣X)IG(Y, X) = H(Y) - H(Y | X)

Where H(Y | X) is the entropy of Y when the attribute X has been given.

H(Y∣X)=∑xP(X=x)H(Y∣X=x)H(Y | X) = \sum_{x} P(X = x) H(Y | X = x)

Based on the definition of entropy(section 2.1), we know:

H(Y∣X=x)=−∑yP(Y=y∣X=x)log⁡2P(Y=y∣X=x)H(Y | X = x) = - \sum_{y} P(Y = y | X = x) \log_{2} P(Y = y | X = x)

To avoid zero value of P(Y = y | X = x) from given dataset, we should use additive smoothing here:

P(Y=y∣X=x)=∣{i∈D∣iX=x∧iY=y}∣∣{i∈D∣iX=x}∣≈∣{i∈D∣iX=x∧iY=y}∣+1∣{i∈D∣iX=x}∣+∣Y∣P(Y = y | X = x) = \frac{|\{i \in D \mid i_X = x \land i_Y = y\}|}{|\{i \in D \mid i_X = x\}|} \approx \frac{|\{i \in D \mid i_X = x \land i_Y = y\}| + 1}{|\{i \in D \mid i_X = x\}| + |Y|}

3.2 The information gain from Temperature to Play.

Conditional probabilities:

P(P=yes∣T=warm)=4+16+2=58P(P = yes | T = warm) = \frac{4 + 1}{6 + 2} = \frac{5}{8} P(P=no∣T=warm)=38P(P = no | T = warm) = \frac{3}{8} P(P=yes∣T=hot)=1+14+2=13P(P = yes | T = hot) = \frac{1 + 1}{4 + 2} = \frac{1}{3} P(P=no∣T=hot)=23P(P = no | T = hot) = \frac{2}{3}

Conditional entropies:

H(P∣T=warm)=−P(P=yes∣T=warm)log⁡2P(P=yes∣T=warm)−P(P=no∣T=warm)log⁡2P(P=no∣T=warm)=−58log⁡258−38log⁡238≈0.954H(P | T = warm) = - P(P = yes | T = warm) \log_{2} P(P = yes | T = warm) - P(P = no | T = warm) \log_{2} P(P = no | T = warm) = - \frac{5}{8} \log_{2} \frac{5}{8} - \frac{3}{8} \log_{2} \frac{3}{8} \approx 0.954 H(P∣T=hot)=−P(P=yes∣T=hot)log⁡2P(P=yes∣T=hot)−P(P=no∣T=hot)log⁡2P(P=no∣T=hot)=−13log⁡213−23log⁡223≈0.918H(P | T = hot) = - P(P = yes | T = hot ) \log_{2} P(P = yes | T = hot) - P(P = no | T = hot) \log_{2} P(P = no | T = hot) = - \frac{1}{3} \log_{2} \frac{1}{3} - \frac{2}{3} \log_{2} \frac{2}{3} \approx 0.918 H(P∣T)=P(T=warm)H(P∣T=warm)+P(T=hot)H(P∣T=hot)≈35×0.954+25×0.918≈0.94H(P | T) = P(T = warm) H(P|T = warm) + P(T = hot) H(P | T = hot) \approx \frac{3}{5} \times 0.954 + \frac{2}{5} \times 0.918 \approx 0.94

The information gain between temperature to Play:

IG(P,T)=H(P)−H(P,T)≈1−0.94=0.06IG(P, T) = H(P) - H(P, T) \approx 1 - 0.94 = 0.06

3.3 The answer through comparing information gain.

Similarly, we can caculate the information gains for each attribute:

AttributeInformation Gain
Outlook0.13
Temp0.06
Humidity0.08
Windy0.02

By considering the information gains of different attributes, Outlook could also be most useful to predict whether people play gold.

4. Drawbacks of multiple-valued attributes.

So far, we know two approaches to meansure the complexity of a specific attribute and the compexity of the relationship between a specific attribute and the class attribute.

Actually, they are the main ideas of how to select the attribute that best classifies examples in the algorithm ID3.

Let's go back to the dataset of playing golf. At this time, we add an attribute RecordNo.

RecordNoOutlookTemperatureHumidityWindyPlay Golf
1sunnywarmlownoyes
2sunnyhotmediumnono
3rainywarmlowyesno
4overcasthothighnono
5sunnywarmmediumyesyes
6rainyhotmediumyesno
7sunnywarmlownoyes
8overcastwarmlowyesyes
9rainywarmhighnono
10overcasthotmediumyesyes

Now, we use the same approaches mentioned in section 2. and section 3 to evaluate attribute RecordNo.

H(RecordNo)=−10×110log⁡2(110)≈3.32H(RecordNo) = - 10 \times \frac{1}{10} \log_{2}(\frac{1}{10}) \approx 3.32 IG(Play,RecordNo)=H(Play)−H(Play∣RecordNo)=1−10×110(−13log⁡213−23log⁡223)≈0.08 IG(Play, RecordNo) = H(Play) - H(Play | RecordNo) = 1 - 10 \times \frac{1}{10} (- \frac{1}{3} \log_{2} \frac{1}{3} - \frac{2}{3} \log_{2} \frac{2}{3}) \approx 0.08

Compared the extropy or information gain, attribute RecordNo could be quite useful to predict the result.

But, obviously, RecordNo means nothing for the result of whether people play golf or not.

5. What is Information Gain Ratio?

Now, let's talk about information gain ratio, which has been used in the algorithm C4.5, a successor of ID3.

The main idea of information gain ratio is basically considering both of the complexity of the attribute it self and the complexity between the attribute and the target classification.

And the purpose is to reduce the bias that we mentioned in section 4.

5.1 The definition of information gain ratio.

Information gain ratio is the ratio between the information gain and the entropy of given attribute:

GR(Y,X)=IG(Y,X)H(X)GR(Y, X) = \frac{IG(Y, X)}{H(X)}

5.2 The answer through comparing information gain ratio.

AttributeInformation GainEntropyInformation Gain Ratio
RecordNo0.083.320.02
Outlook0.131.570.08
Temperature0.060.970.06
Humidity0.081.520.05
Windy0.0210.02

By considering the information gain ratios of different attributs, Outlook could be most useful to predict whether people play golf now.

References

COMMAND INTERFACE

↑ ↓ navigate · Enter execute · Esc exit · try “whoami”

Loading filesystem…

:help aguang.xyz

A personal website disguised as an operating system.

Ctrl / Cmd + K
Command palette + full-text search
Tab / Shift + Tab
Move between links and controls
Enter
Open a focused link or search result
↑ / ↓ in search
Navigate search results
In a directory filter
Enter/Done or ↓ focuses a result; Esc clears the filter
Esc
Clear a search or close a window