Build a one-row class imbalance report from a pandas label Series
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Implement `class_imbalance_report(s: pd.Series) -> pd.DataFrame`, which summarizes label imbalance in a one-row DataFrame with the default `RangeIndex` `[0]`. The columns, in order, are:
| Column | dtype |
|---|---|
| `majority_level` | object |
| `majority_count` | int64 |
| `minority_level` | object |
| `minority_count` | int64 |
| `imbalance_ratio` | float64 |
| `severity` | object |
Requirements:
- Exclude `NaN` before counting levels.
- The majority is the level with the highest count and the minority is the level with the lowest count. On a tie, pick the alphabetically first level.
- `imbalance_ratio = majority_count / minority_count`, as float64.
- `severity` is `'minor'` when the ratio is below 3, `'moderate'` from 3 up to but not including 10, `'severe'` from 10 up to but not including 100, and `'extreme'` at 100 or more.
- Column order and dtypes must match exactly. The result is checked with `assert_frame_equal`, with a float tolerance on `imbalance_ratio`.
Example:
```python
s = pd.Series(['a','a','a','a','a','a','a','a','a','a','a','a','b','c'])
# counts: a=12, b=1, c=1 -> minority tie between b and c, 'b' wins
class_imbalance_report(s)
# majority_level majority_count minority_level minority_count imbalance_ratio severity
# 0 a 12 b 1 12.0 severe
```
```hint Ties
`value_counts` gives you the counts, but it does not promise any order among levels with equal counts. Decide what you sort on so the tie rule holds every time.
```
```hint Dtypes
`assert_frame_equal` compares dtypes as well as values. Check what dtype each column gets when you build the frame from scalars pulled out of other pandas objects.
```
### Constraints & Assumptions
- The Series has at least two distinct non-null levels.
- `NaN` values may appear and do not count as a level.
- The output must pass `assert_frame_equal` against the expected frame, so column order, dtypes and the index all have to match.
### What a Strong Answer Covers
- Counting levels with `NaN` excluded
- A deterministic tie-break for both the majority and the minority pick that follows the alphabetical rule
- Correct `severity` bands at the exact boundaries 3, 10 and 100
- Building the output with the exact column order, dtypes and a `RangeIndex` starting at 0
- Time complexity and edge cases, such as every level having the same count
### Follow-up Questions
- How would you extend the report to list every level with its count and its ratio against the majority, while still giving one overall severity?
- If the levels come from a categorical dtype with an unused category, the minority count could be 0. What happens to the ratio, and how would you guard against it?
- How would each severity band change the way you train and evaluate a classifier on this data?
Overview: Write a pandas function that summarizes label imbalance in a one-row DataFrame with the majority and minority classes, their counts, the ratio between them and a severity band. It tests value_counts, deterministic tie-breaking and exact dtype control for assert_frame_equal.