Score class imbalance in a pandas column with normalized entropy

Quick Overview

Implement a pandas function that scores how imbalanced a categorical column is using normalized Shannon entropy from scipy. It tests null handling, the edge cases where a column has zero or one level, floating-point care on uniform columns, and how entropy compares with the majority-class share.

Score class imbalance in a pandas column with normalized entropy

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `class_balance_score(s: pd.Series) -> float` returning `1 - entropy(counts, base=2) / log2(k)` as a Python `float`, where `counts` are the level frequencies of the non-null values and `k` is the number of distinct levels. Use `scipy.stats.entropy` (raw counts may be passed directly). The function must follow these rules: - Drop nulls before counting. - A uniform column returns `0.0`. - When `k == 1`, return `1.0` (no warning, no `nan`). Example: ```python class_balance_score(pd.Series(['a','b','c','d'])) # -> 0.0 (uniform) class_balance_score(pd.Series(['a']*99 + ['b'])) # -> ~0.92 (skewed) class_balance_score(pd.Series(['a','a','a'])) # -> 1.0 (single level) ``` ```hint Look at the denominator Work out what `log2(k)` equals for the smallest possible values of `k` before you divide by it. ``` ```hint Floating point on a uniform column Compute the score by hand in floating point for a uniform column with three levels and ask whether the result is guaranteed to be exactly zero. ``` ### What a Strong Answer Covers - Dropping nulls and counting level frequencies, with `k` taken from the non-null levels only. - Why dividing by `log2(k)` puts the score in the range 0 to 1, and what each end of the range means. - Guards for `k == 1` and for an all-null Series, so the function never divides by zero or returns `nan` where the contract forbids it. - Returning a plain Python `float`, with an exact `0.0` for a uniform column. - Time and space complexity. ### Follow-up Questions - How would you handle an entirely empty (all-null) Series? - Why use entropy instead of the majority-class proportion to measure imbalance? - Is the score comparable between a binary column and a column with 50 levels? What would you change if you needed one threshold for both?

Overview: Implement a pandas function that scores how imbalanced a categorical column is using normalized Shannon entropy from scipy. It tests null handling, the edge cases where a column has zero or one level, floating-point care on uniform columns, and how entropy compares with the majority-class share.

General logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement class_balance_score(s: pd.Series) -> float returning 1 - entropy(counts, base=2) / log2(k) as a Python float, where counts are the level frequencies of the non-null values and k is the number of distinct levels. Use scipy.stats.entropy (raw counts may be passed directly).

The function must follow these rules:

  • Drop nulls before counting.
  • A uniform column returns 0.0 .
  • When k == 1 , return 1.0 (no warning, no nan ).

Example:

class_balance_score(pd.Series(['a','b','c','d']))        # -> 0.0  (uniform)
class_balance_score(pd.Series(['a']*99 + ['b']))         # -> ~0.92 (skewed)
class_balance_score(pd.Series(['a','a','a']))            # -> 1.0  (single level)

What a Strong Answer Covers Guidance

  • Dropping nulls and counting level frequencies, with k taken from the non-null levels only.
  • Why dividing by log2(k) puts the score in the range 0 to 1, and what each end of the range means.
  • Guards for k == 1 and for an all-null Series, so the function never divides by zero or returns nan where the contract forbids it.
  • Returning a plain Python float , with an exact 0.0 for a uniform column.
  • Time and space complexity.

Follow-up Questions Guidance

  • How would you handle an entirely empty (all-null) Series?
  • Why use entropy instead of the majority-class proportion to measure imbalance?
  • Is the score comparable between a binary column and a column with 50 levels? What would you change if you needed one threshold for both?