v9.1.1
Menu

Information Gain

easyFREEDecision TreesMachine Learning 1

Compute the information gain from splitting a dataset.

Given a parent set of labels and the labels in two child subsets after a split, compute:

IG=H(parent)−∣left∣∣parent∣H(left)−∣right∣∣parent∣H(right)IG = H(parent) - \frac{|left|}{|parent|} H(left) - \frac{|right|}{|parent|} H(right)

where HH is the entropy: H=−∑k=1Kpklog⁡2(pk)H = -\sum_{k=1}^{K} p_k \log_2(p_k)

Use 0log⁡2(0)=00 \log_2(0) = 0 by convention. Return the information gain rounded to 4 decimal places.

Example

Input
parent = [1, 1, 0, 0]
left = [1, 1]
right = [0, 0]
Output
1.0
Reasoning
  • Inputs: parent holds the 4 class labels before the split (two 1s and two 0s); left and right are the two groups they were divided into, here [1, 1] and [0, 0].
  • Parent entropy: each class has proportion p=24=12p = \frac{2}{4} = \frac{1}{2}, so H(parent)=−(12log⁡212+12log⁡212)=−(−12−12)=1H(parent) = -\left(\frac{1}{2}\log_2\frac{1}{2} + \frac{1}{2}\log_2\frac{1}{2}\right) = -\left(-\frac{1}{2} - \frac{1}{2}\right) = 1 bit, the maximum for two classes.
  • Child entropies: each child is pure, so its only class has p=1p = 1 and H=−1⋅log⁡21=0H = -1 \cdot \log_2 1 = 0. Both H(left)H(left) and H(right)H(right) are 00.
  • Weighting: each child holds 2 of the 4 labels, so both weights are 24=0.5\frac{2}{4} = 0.5. The weighted child entropy is 0.5⋅0+0.5⋅0=00.5 \cdot 0 + 0.5 \cdot 0 = 0.
  • Output: IG=1−0=1IG = 1 - 0 = 1, shown rounded to 4 decimal places as 1.0.
  • Notice that a perfect split removes all of the parent's uncertainty. Splitting the same parent into {1,0}\{1, 0\} and {1,0}\{1, 0\} instead leaves each child at 1 bit, giving a gain of 00.

Constraints

  • parent, left, right are lists of class labels
  • left and right together form the parent
  • Return information gain rounded to 4 decimal places
  • Use log base 2

Learn this in

More from Machine Learning 1

solution.py

Test results

Sign in to run your code against the test cases.