Information gain has a flaw: a feature with many distinct values (an ID column, say) splits the data into tiny, perfectly pure groups and looks wonderful, while telling you nothing about unseen samples. Gain ratio divides the gain by how finely the feature splits the data, so many-valued features are penalised.
Implement gain_ratio(feature, labels). Split labels into one group per distinct value of feature, compute the information gain of that split, then divide by the split's intrinsic value. Use base-2 logarithms throughout.
feature and labels are one-dimensional arrays of equal length; either can hold arbitrary integers.float.0.0 for empty input, and 0.0 when the feature has a single distinct value (its intrinsic value is zero).Open one at a time. Each gives away a little more than the last.
Entropy is -sum(p * log2(p)) over class proportions. Information gain is the parent entropy minus the size-weighted entropy of the child groups.
The intrinsic value is the entropy of the group sizes themselves: -sum(w * log2(w)), where w is each group's share of the samples.
No results yet
Run your code to check it against the examples, or submit to run every test.