Optimal Binary Split for Categorical Features

Quick Overview

Implement Gini impurity and the best binary subset split of a categorical feature for a 0/1 target by ordering categories by positive rate and scanning prefixes. It tests impurity arithmetic, tie rules, and why a prefix scan matches an exhaustive subset search.

Optimal Binary Split for Categorical Features

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `gini_impurity(y)` (Gini of any label array, `0.0` when empty) and `best_categorical_split(x, y)`. Sort the distinct categories of `x` by positive rate ascending (ties: ascending category value), evaluate the K-1 prefix splits, and choose the one minimising weighted child Gini. Return `{"left": list, "impurity": float, "gain": float}`: `left` is the prefix (low-rate) categories sorted ascending, `impurity` is the size-weighted child Gini, `gain` is parent Gini minus `impurity`. On equal impurity the shortest prefix wins. Return `None` if fewer than 2 categories. For example: ```python x = ['a','a','a','b','b','c','c','c','c','d'] y = [ 0, 0, 1, 1, 1, 0, 0, 0, 0, 1] # rates: c=0, a=1/3, b=1, d=1 -> order c, a, b, d best_categorical_split(x, y) # {'left': ['a', 'c'], 'impurity': 6/35 (~0.1714), 'gain': 0.48 - 6/35 (~0.3086)} ``` ```hint Count once Everything the prefix scan needs can come from per-category row counts and positive counts, accumulated in the sorted order. ``` ```hint Two tie rules One tie rule orders categories that share a rate, the other picks between prefixes with equal impurity. Check that your sort and your comparison each respect theirs. ``` ### Constraints & Assumptions - `x` and `y` are equal-length NumPy arrays. `x` holds sortable values (ints or strings). - `y` for the split is binary (0/1). Every category appears at least once. - Results are checked against brute-force subset search with tolerance `1e-7`. ### What a Strong Answer Covers - A Gini impurity that works for any label array, including the empty case - Ordering categories by positive rate with the stated tie rule - The weighted child impurity of each of the K-1 prefixes, computed without rescanning the rows - The returned `left` list, the shortest-prefix tie rule, and the `None` case - Why the prefix scan matches an exhaustive subset search, and the time complexity ### Follow-up Questions - Why does sorting by positive rate guarantee the optimal subset among all 2^(K-1)-1 splits? - How would you extend this to multiclass targets? - What overfitting risks do high-cardinality categorical splits introduce, and how would you mitigate them?

Overview: Implement Gini impurity and the best binary subset split of a categorical feature for a 0/1 target by ordering categories by positive rate and scanning prefixes. It tests impurity arithmetic, tie rules, and why a prefix scan matches an exhaustive subset search.

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

Implement gini_impurity(y) (Gini of any label array, 0.0 when empty) and best_categorical_split(x, y). Sort the distinct categories of x by positive rate ascending (ties: ascending category value), evaluate the K-1 prefix splits, and choose the one minimising weighted child Gini.

Return {"left": list, "impurity": float, "gain": float}: left is the prefix (low-rate) categories sorted ascending, impurity is the size-weighted child Gini, gain is parent Gini minus impurity. On equal impurity the shortest prefix wins. Return None if fewer than 2 categories.

For example:

x = ['a','a','a','b','b','c','c','c','c','d']
y = [ 0,  0,  1,  1,  1,  0,  0,  0,  0,  1]
# rates: c=0, a=1/3, b=1, d=1 -> order c, a, b, d
best_categorical_split(x, y)
# {'left': ['a', 'c'], 'impurity': 6/35 (~0.1714), 'gain': 0.48 - 6/35 (~0.3086)}

Constraints & Assumptions

  • x and y are equal-length NumPy arrays. x holds sortable values (ints or strings).
  • y for the split is binary (0/1). Every category appears at least once.
  • Results are checked against brute-force subset search with tolerance 1e-7 .

What a Strong Answer Covers Guidance

  • A Gini impurity that works for any label array, including the empty case
  • Ordering categories by positive rate with the stated tie rule
  • The weighted child impurity of each of the K-1 prefixes, computed without rescanning the rows
  • The returned left list, the shortest-prefix tie rule, and the None case
  • Why the prefix scan matches an exhaustive subset search, and the time complexity

Follow-up Questions Guidance

  • Why does sorting by positive rate guarantee the optimal subset among all 2^(K-1)-1 splits?
  • How would you extend this to multiclass targets?
  • What overfitting risks do high-cardinality categorical splits introduce, and how would you mitigate them?