Implement C4.5 gain ratio and pick the best categorical split feature
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement the split scores C4.5 uses to choose a categorical feature. For a multiway split on a categorical feature `x` with labels `y`, write `entropy(y)`, `information_gain(x, y)`, `split_information(x)` and `gain_ratio(x, y)`, each returning a Python `float`. Then write `best_feature(X, y, criterion="gain_ratio")`, which scores every column of `X` with the chosen criterion and returns the index of the column with the highest score.
```python
y = [0] * 6 + [1] * 6
ids = [f"id{i}" for i in range(12)] # all unique
binary = list("aaaaab") + list("abbbbb")
const = ["c"] * 12
information_gain(ids, y) # 1.0
gain_ratio(ids, y) # 1 / log2(12) ≈ 0.279
gain_ratio(binary, y) # 1 - H(1/6, 5/6) ≈ 0.350
X = np.array([ids, binary, const], dtype=object).T # shape (12, 3)
best_feature(X, y, "information_gain") # 0
best_feature(X, y, "gain_ratio") # 1
```
```hint Work the ID column by hand
Every quantity here is a base-2 entropy of some counts. For the ID column, ask what each child's label entropy is and what the entropy of twelve equal-sized categories is.
```
### Constraints & Assumptions
- All entropies use log base 2. `split_information(x)` is the entropy of the category proportions of `x`.
- If the split information is 0 (a constant feature), `gain_ratio` returns `0.0` without raising.
- Labels and categories may be ints or strings, and results must not depend on the category names.
- `criterion` is `"gain_ratio"` or `"information_gain"`. Any other value raises `ValueError`. Ties go to the lowest column index.
- `X` may be an object array whose columns hold different types. Scores must match the expected values within a relative tolerance of `1e-7`.
### What a Strong Answer Covers
- A base-2 entropy computed from value counts, reused for the label distribution and for the split information.
- Information gain as the parent entropy minus the size-weighted child entropies of the multiway split, matching the worked numbers.
- A constant feature that gets a gain ratio of 0.0 without a division by zero.
- A `best_feature` that validates the criterion, handles object columns, and keeps the lowest index on ties.
- Why information gain picks the ID column while gain ratio picks the binary one.
### Follow-up Questions
- Why can gain ratio over-favor a feature with very low split information, and how does C4.5 guard against it?
- How would you extend these scores to continuous features?
- CART uses binary splits and Gini impurity. Does it need a gain-ratio-style correction for many-valued categorical features?
Overview: Implement entropy, information gain, split information and the C4.5 gain ratio for multiway splits on categorical features, then pick the best column to split on. It tests the entropy formulas, the zero split information case, tie handling, and why gain ratio penalizes ID-like features.