📘
Information Gain
EasyDecision Trees
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)−∣parent∣∣left∣H(left)−∣parent∣∣right∣H(right)
where H is the entropy: H=−∑k=1Kpklog2(pk)
Use 0log2(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:
- First, we calculate the entropy of the parent set: H(parent)=−(42log2(42)+42log2(42))=−(21log2(21)+21log2(21))=1
- Then, we calculate the entropy of the left and right child sets: H(left)=−(22log2(22))=0 and H(right)=−(22log2(22))=0
- Next, we apply the information gain formula: IG=H(parent)−∣parent∣∣left∣H(left)−∣parent∣∣right∣H(right)=1−42⋅0−42⋅0=1
- The final output is 1.0 after rounding to 4 decimal places
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
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.