Implement a pandas function that builds a lift matrix comparing how often each target class appears within each level of a categorical feature against its overall rate. It tests crosstab normalization, label-aligned division, axis naming and ordering, and how to read lift values.
Implement `category_class_lift_matrix(df, cat_col, target)`. For each level of `cat_col`, return the lift of each `target` class: `P(class | level) / P(class)`. Compute `P(class | level)` with `pd.crosstab(df[cat_col], df[target], normalize='index')` and `P(class)` with `df[target].value_counts(normalize=True)`.
The result must meet these requirements:
- It is a DataFrame of `float64` lifts. A value of `1.0` means the level matches the population mix for that class, and `2.0` means the class is twice as concentrated in that level.
- Rows are the levels of `cat_col` sorted ascending. Columns are the target classes sorted ascending.
- The row index has `name == cat_col` and the column index has `name == target`.
For example, with `cat_col="region"` and `target="plan"`, suppose the four `east` rows have plans `basic`, `basic`, `pro`, `team` and the four `west` rows have `basic`, `pro`, `pro`, `pro`. The overall rates are 0.375 for `basic`, 0.5 for `pro` and 0.125 for `team`, and the function returns:
```text
plan basic pro team
region
east 1.333333 0.5 2.0
west 0.666667 1.5 0.0
```
```hint Alignment
Dividing a DataFrame by a Series lines values up by label. Work out which axis of the crosstab the base-rate Series has to match, and whether its order matters.
```
```hint Sanity check
For each class, weight every level's lift by that level's share of the rows and add them up. Think about what total you should get.
```
### What a Strong Answer Covers
- The conditional distribution per level and the overall class rates, computed as the prompt specifies.
- A division that scales each class column by that class's own base rate.
- Axis order, axis names and `float64` values that match the contract.
- How to read the values (1.0, above 1, below 1, exactly 0) and what happens with zero cells and missing values.
### Follow-up Questions
- How would you handle a level or class that never co-occurs (a zero cell)?
- Why is lift more informative than raw `P(class | level)` here?
- A level with only three rows shows a lift of 3.0 for one class. How would you decide whether to trust it?
Overview: Implement a pandas function that builds a lift matrix comparing how often each target class appears within each level of a categorical feature against its overall rate. It tests crosstab normalization, label-aligned division, axis naming and ordering, and how to read lift values.
Implement category_class_lift_matrix(df, cat_col, target). For each level of cat_col, return the lift of each target class: P(class | level) / P(class). Compute P(class | level) with pd.crosstab(df[cat_col], df[target], normalize='index') and P(class) with df[target].value_counts(normalize=True).
The result must meet these requirements:
It is a DataFrame of
float64
lifts. A value of
1.0
means the level matches the population mix for that class, and
2.0
means the class is twice as concentrated in that level.
Rows are the levels of
cat_col
sorted ascending. Columns are the target classes sorted ascending.
The row index has
name == cat_col
and the column index has
name == target
.
For example, with cat_col="region" and target="plan", suppose the four east rows have plans basic, basic, pro, team and the four west rows have basic, pro, pro, pro. The overall rates are 0.375 for basic, 0.5 for pro and 0.125 for team, and the function returns:
plan basic pro team
region
east 1.333333 0.5 2.0
west 0.666667 1.5 0.0
What a Strong Answer Covers Guidance
The conditional distribution per level and the overall class rates, computed as the prompt specifies.
A division that scales each class column by that class's own base rate.
Axis order, axis names and
float64
values that match the contract.
How to read the values (1.0, above 1, below 1, exactly 0) and what happens with zero cells and missing values.
Follow-up Questions Guidance
How would you handle a level or class that never co-occurs (a zero cell)?
Why is lift more informative than raw
P(class | level)
here?
A level with only three rows shows a lift of 3.0 for one class. How would you decide whether to trust it?