Skip to content
DeepForgeby svx
All problems
HardInformation Theoryinfo-354

Information Gain Ratio of a Split

Problem

Compute the information gain ratio of a split, the information gain in bits divided by the split information, the entropy of the group-size proportions. This penalizes splits that create many small groups. Return 0.0 when groups is empty, labels is empty, or the split information is zero.

Hint

Compute the ordinary gain first, then divide by the entropy of the group-size distribution.

Test cases

case 1
in: [["a","a","b","b"],[[0,1],[2,3]]]
expected: 1
case 2
in: [["a","a","b","b"],[[0,1,2,3]]]
expected: 0
case 3
in: [["a","a","b","b","c"],[[0,1,2],[3,4]]]
expected: 0.5880325916843803
case 4
in: [["a","b","c","d","e"],[[0],[1],[2],[3],[4]]]
expected: 1
case 5
in: [["a","b"],[]]
expected: 0
Starter codePython
import math
def gain_ratio(labels, groups):
    # Your code here
    pass

5 test cases · run your code against them in the editor