Find the best ID3 split by information gain on categorical features

Quick Overview

Implement base-2 entropy, information gain for a multiway categorical split, and an ID3 best-split finder that returns the winning feature, its gain and the row indices of each branch. Tests the entropy formulas, deterministic tie-breaking with a tolerance, and type-safe grouping of labels and values.

Find the best ID3 split by information gain on categorical features

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement three functions. `entropy(y)` returns the base-2 Shannon entropy of a label array. `information_gain(x, y)` returns the gain of a multiway split of `y` on categorical column `x`. `best_split(X, y, used=None)` returns `(feature_index, gain, branches)` for the feature (not in `used`) with the highest gain. Example: ```python X = [['a', 'x'], ['a', 'y'], ['b', 'y'], ['b', 'x']] y = [0, 0, 1, 1] best_split(X, y) # (0, 1.0, {'a': array([0, 1]), 'b': array([2, 3])}) best_split(X, y, used={0}) # (None, 0.0, {}) best_split([['a', 'u'], ['b', 'v']], [0, 1]) # (0, 1.0, {'a': array([0]), 'b': array([1])}) ``` In the first call, feature 0 determines the label completely, while each value of feature 1 holds one row of each class. In the second call, feature 0 is excluded and feature 1 has zero gain. In the third, both features have gain 1.0 and the tie goes to index 0. ```hint Counting without sorting Labels can be any hashable type and a column can hold strings or integers. Pick counting and grouping tools that work for all of them without having to order the values. ``` ### Constraints & Assumptions - `entropy` of an empty array is `0.0`. Return Python `float`s. - Gain ties go to the lowest feature index. - If no unused feature has gain > 0 (within `1e-12`), return `(None, 0.0, {})`. - `branches` maps each distinct value of the chosen feature to an `int` array of ascending row indices. - `X` may hold strings or integers. Labels may be any hashable type. ### What a Strong Answer Covers - Entropy from class counts in base 2, including the empty and pure cases. - How gain is computed from the parent and child entropies. - Skipping features in `used`, and how ties and zero-gain splits are handled under the `1e-12` tolerance. - How the branches are built so every allowed value and label type works, with ascending row indices. - The time cost per feature and overall. ### Follow-up Questions - Why does information gain favour high-cardinality features, and how does C4.5 fix it? - How would you use `best_split` to build the full ID3 tree? - How should the finished tree handle a categorical value at prediction time that never appeared in training?

Overview: Implement base-2 entropy, information gain for a multiway categorical split, and an ID3 best-split finder that returns the winning feature, its gain and the row indices of each branch. Tests the entropy formulas, deterministic tie-breaking with a tolerance, and type-safe grouping of labels and values.

PracHub logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement three functions. entropy(y) returns the base-2 Shannon entropy of a label array. information_gain(x, y) returns the gain of a multiway split of y on categorical column x. best_split(X, y, used=None) returns (feature_index, gain, branches) for the feature (not in used) with the highest gain.

Example:

X = [['a', 'x'], ['a', 'y'], ['b', 'y'], ['b', 'x']]
y = [0, 0, 1, 1]
best_split(X, y)
# (0, 1.0, {'a': array([0, 1]), 'b': array([2, 3])})

best_split(X, y, used={0})
# (None, 0.0, {})

best_split([['a', 'u'], ['b', 'v']], [0, 1])
# (0, 1.0, {'a': array([0]), 'b': array([1])})

In the first call, feature 0 determines the label completely, while each value of feature 1 holds one row of each class. In the second call, feature 0 is excluded and feature 1 has zero gain. In the third, both features have gain 1.0 and the tie goes to index 0.

Constraints & Assumptions

  • entropy of an empty array is 0.0 . Return Python float s.
  • Gain ties go to the lowest feature index.
  • If no unused feature has gain > 0 (within 1e-12 ), return (None, 0.0, {}) .
  • branches maps each distinct value of the chosen feature to an int array of ascending row indices.
  • X may hold strings or integers. Labels may be any hashable type.

What a Strong Answer Covers Guidance

  • Entropy from class counts in base 2, including the empty and pure cases.
  • How gain is computed from the parent and child entropies.
  • Skipping features in used , and how ties and zero-gain splits are handled under the 1e-12 tolerance.
  • How the branches are built so every allowed value and label type works, with ascending row indices.
  • The time cost per feature and overall.

Follow-up Questions Guidance

  • Why does information gain favour high-cardinality features, and how does C4.5 fix it?
  • How would you use best_split to build the full ID3 tree?
  • How should the finished tree handle a categorical value at prediction time that never appeared in training?