Screen numeric features for a monotonic binned target rate in pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Implement `monotonic_screen(df, target, q=4)` as a quick screen for scorecard features. For each numeric column except the binary 0/1 `target`, bin the column with `pd.qcut(..., q, duplicates='drop')`, compute the mean of `target` per bin in ascending bin order, and classify that rate series.
The output must follow these rules:
- `direction` is `'increasing'` if the rate series is non-decreasing. That check comes first, so a flat series is `'increasing'`. Otherwise it is `'decreasing'` if the series is non-increasing, and `'non-monotonic'` if neither holds.
- `n_bins` (int64) is the number of bins actually produced, counting only bins that hold at least one row. It can be smaller than `q` when tied values make `qcut` drop edges.
- `rate_spread` (float64) is the maximum bin rate minus the minimum bin rate.
- The columns are exactly `['feature', 'n_bins', 'rate_spread', 'direction']`, with `feature` and `direction` as object dtype.
- Sort by `rate_spread` descending, then `feature` ascending, then call `reset_index(drop=True)`.
For example:
```python
df = pd.DataFrame({
"utilization": [10, 20, 30, 40, 50, 60, 70, 80],
"age": [31, 34, 41, 22, 25, 45, 58, 63],
"tenure_years": [4, 6, 0, 0, 0, 0, 0, 0],
"default": [0, 0, 0, 1, 1, 0, 1, 1],
})
monotonic_screen(df, "default", q=4)
```
returns:
```text
feature n_bins rate_spread direction
0 age 4 1.000000 non-monotonic
1 utilization 4 1.000000 increasing
2 tenure_years 2 0.666667 decreasing
```
`utilization` splits into four bins of two rows with default rates 0.0, 0.5, 0.5 and 1.0, which is non-decreasing. `age` gives the U shape 1.0, 0.0, 0.0, 1.0. Six of the eight `tenure_years` values are 0, so three of the quantile edges coincide and only two bins survive, with rates 4/6 and 0.0. `age` and `utilization` tie on `rate_spread`, so `age` comes first by name.
```hint Keep the bins in order
`qcut` returns an ordered categorical. Group the target by it and make sure the resulting rate series follows bin order and leaves out empty bins.
```
```hint A flat series passes both checks
pandas' monotonic flags are non-strict, so a series with equal steps satisfies both. The order in which you test them decides the label.
```
### Clarifying Questions to Ask
- Should a column with a single distinct value, where no quantile bins can form, appear in the output?
- Can the target or the features contain missing values, and should those rows be dropped?
- Is the target stored as an integer 0/1 column or as a boolean column?
### What a Strong Answer Covers
- Excludes the target and bins every other numeric column with `qcut` and `duplicates='drop'`.
- Computes the target rate for each non-empty bin in ascending bin order.
- Applies the direction checks in the stated order, so a flat series is labeled `'increasing'`.
- Reports `n_bins` and `rate_spread` with the exact column order, dtypes and sort order.
- Explains why raw monotonicity on a few bins can mislead, for example with one bin or one small reversal.
### Follow-up Questions
- How would you handle a feature that produces only one bin after `duplicates='drop'`?
- Would you trust raw monotonicity, or add a tolerance or a statistical test?
- What would you do with a strong but U-shaped feature like `age` if the scorecard needs monotone bins?
- How does the choice of `q` change which features come out as `'non-monotonic'`?
Overview: Write a pandas function that bins each numeric feature into quantiles, computes the binary target rate per bin and labels the trend as increasing, decreasing or non-monotonic. It tests pd.qcut with tied values, grouped means, monotonicity checks and deterministic sorting.