Class Imbalance Ratio

Quick Overview

Write a pandas function that returns the ratio of the most frequent to the least frequent class count in a label Series, ignoring missing values. It tests value counting and careful handling of empty, single-level and categorical inputs.

Class Imbalance Ratio

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `class_imbalance_ratio(s: pd.Series) -> float`, which returns the count of the most frequent level divided by the count of the least frequent level, after excluding NaN. The function must meet these requirements: - A balanced Series, or one with exactly one distinct level, returns `1.0`. - An empty or all-NaN Series returns `float('nan')`. - The return value is a scalar Python or NumPy float that passes `numpy.testing.assert_allclose` (with `equal_nan=True` for the empty case). ```python # 40 of label 'A', 4 of label 'B' s = pd.Series(['A'] * 40 + ['B'] * 4) class_imbalance_ratio(s) # -> 10.0 class_imbalance_ratio(pd.Series(['x', 'x', 'y', 'y'])) # -> 1.0 class_imbalance_ratio(pd.Series([], dtype=object)) # -> nan ``` ```hint Start from the counts Reduce the Series to one count per level first, then check which required case each kind of input lands in. Also look at what a pandas categorical Series puts into those counts. ``` ### What a Strong Answer Covers - Counts levels with NaN excluded and divides the largest count by the smallest - Returns NaN for an empty or all-NaN Series without raising - Explains why a single level and a balanced Series both give 1.0 - Returns a plain float and copes with categorical input whose unused categories have zero counts ### Follow-up Questions - How would the ratio guide your choice between class weights, resampling and threshold tuning? - The ratio only looks at the two extreme classes. What would you report for a multiclass target where several classes are small? - How would you compute the ratio per day to monitor imbalance over time, and what can mislead you there?

Overview: Write a pandas function that returns the ratio of the most frequent to the least frequent class count in a label Series, ignoring missing values. It tests value counting and careful handling of empty, single-level and categorical inputs.

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

Implement class_imbalance_ratio(s: pd.Series) -> float, which returns the count of the most frequent level divided by the count of the least frequent level, after excluding NaN.

The function must meet these requirements:

  • A balanced Series, or one with exactly one distinct level, returns 1.0 .
  • An empty or all-NaN Series returns float('nan') .
  • The return value is a scalar Python or NumPy float that passes numpy.testing.assert_allclose (with equal_nan=True for the empty case).
# 40 of label 'A', 4 of label 'B'
s = pd.Series(['A'] * 40 + ['B'] * 4)
class_imbalance_ratio(s)  # -> 10.0

class_imbalance_ratio(pd.Series(['x', 'x', 'y', 'y']))  # -> 1.0
class_imbalance_ratio(pd.Series([], dtype=object))      # -> nan

What a Strong Answer Covers Guidance

  • Counts levels with NaN excluded and divides the largest count by the smallest
  • Returns NaN for an empty or all-NaN Series without raising
  • Explains why a single level and a balanced Series both give 1.0
  • Returns a plain float and copes with categorical input whose unused categories have zero counts

Follow-up Questions Guidance

  • How would the ratio guide your choice between class weights, resampling and threshold tuning?
  • The ratio only looks at the two extreme classes. What would you report for a multiclass target where several classes are small?
  • How would you compute the ratio per day to monitor imbalance over time, and what can mislead you there?