Build group-by aggregate features with defaults for unseen keys

Quick Overview

Build per-group count, mean, max and standard deviation features fitted on training data only and mapped onto both train and test frames, with defaults for unseen or missing keys. It tests leakage-safe feature engineering with pandas groupby and map, column naming and dtype rules.

Build group-by aggregate features with defaults for unseen keys

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `build_group_aggregates(train, test, group_cols, value_col)`. For each column in `group_cols`, compute count, mean, max and std of `value_col` per group using `train` only. Map those statistics onto every row of both frames and return `(train_out, test_out)`. The function must follow these rules: - Append columns named `{col}_{value_col}_{stat}` after the original columns, ordered by `group_cols`, then `count, mean, max, std`. - Std is population std (`ddof=0`). All new columns are `float64`. - Unseen or missing keys get count `0.0`, and the global train mean, max and population std for the other stats. - Test values never influence features. Inputs are not mutated and indexes are preserved. For example: ```python train = pd.DataFrame({ "customer_id": ["c1", "c1", "c2"], "merchant_id": ["m1", "m2", "m1"], "amount": [10, 30, 50], }) test = pd.DataFrame({"customer_id": ["c9"], "merchant_id": ["m1"], "amount": [999]}) train_out, test_out = build_group_aggregates( train, test, ["customer_id", "merchant_id"], "amount" ) # train row 0 (c1): customer_id_amount_count=2.0, _mean=20.0, _max=30.0, _std=10.0 # test row (c9, unseen): customer_id_amount_count=0.0, _mean=30.0, _max=50.0, _std=16.33 # (16.33 is np.std([10, 30, 50])) # test row (m1, seen): merchant_id_amount_count=2.0, _mean=30.0, _max=50.0, _std=20.0 ``` Customer `c9` never appears in `train`, so its customer features fall back to the global train statistics. The test amount of 999 plays no part in any feature. ```hint Fit once, apply twice Build the per-group lookup tables from `train` alone, then apply the same tables to both frames. Check what your lookup returns for a key it has never seen and for a missing key. ``` ### What a Strong Answer Covers - Fitting every statistic on `train` only and applying the same lookup tables to both frames. - Exact column names, column order and the `float64` dtype. - Population standard deviation, including the value a one-row group gets. - Defaults for unseen and missing keys. - Leaving the inputs unmodified and the indexes intact. ### Follow-up Questions - Train rows see their own value in the aggregate. How would you avoid that target-style leakage? - Groups with a count of 1 have noisy means. How would you stabilise them?

Overview: Build per-group count, mean, max and standard deviation features fitted on training data only and mapped onto both train and test frames, with defaults for unseen or missing keys. It tests leakage-safe feature engineering with pandas groupby and map, column naming and dtype rules.

General logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement build_group_aggregates(train, test, group_cols, value_col). For each column in group_cols, compute count, mean, max and std of value_col per group using train only. Map those statistics onto every row of both frames and return (train_out, test_out).

The function must follow these rules:

  • Append columns named {col}_{value_col}_{stat} after the original columns, ordered by group_cols , then count, mean, max, std .
  • Std is population std ( ddof=0 ). All new columns are float64 .
  • Unseen or missing keys get count 0.0 , and the global train mean, max and population std for the other stats.
  • Test values never influence features. Inputs are not mutated and indexes are preserved.

For example:

train = pd.DataFrame({
    "customer_id": ["c1", "c1", "c2"],
    "merchant_id": ["m1", "m2", "m1"],
    "amount": [10, 30, 50],
})
test = pd.DataFrame({"customer_id": ["c9"], "merchant_id": ["m1"], "amount": [999]})
train_out, test_out = build_group_aggregates(
    train, test, ["customer_id", "merchant_id"], "amount"
)

# train row 0 (c1): customer_id_amount_count=2.0, _mean=20.0, _max=30.0, _std=10.0
# test row (c9, unseen): customer_id_amount_count=0.0, _mean=30.0, _max=50.0, _std=16.33
#   (16.33 is np.std([10, 30, 50]))
# test row (m1, seen): merchant_id_amount_count=2.0, _mean=30.0, _max=50.0, _std=20.0

Customer c9 never appears in train, so its customer features fall back to the global train statistics. The test amount of 999 plays no part in any feature.

What a Strong Answer Covers Guidance

  • Fitting every statistic on train only and applying the same lookup tables to both frames.
  • Exact column names, column order and the float64 dtype.
  • Population standard deviation, including the value a one-row group gets.
  • Defaults for unseen and missing keys.
  • Leaving the inputs unmodified and the indexes intact.

Follow-up Questions Guidance

  • Train rows see their own value in the aggregate. How would you avoid that target-style leakage?
  • Groups with a count of 1 have noisy means. How would you stabilise them?