Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs
Mean aggregation is a simple feature-engineering trick where you summarize a group of values with their average. In this task, you’ll build a function that creates per-group mean features and attaches them back to each row.
Implement the function
Rules:
Output:
| Argument | Type |
|---|---|
| rows | np.ndarray |
| value_idx | int |
| group_key_idx | int |
| Return Name | Type |
|---|---|
| value | np.ndarray |
No pandas; only Python/NumPy allowed
Return NumPy array
Preserve original row order
Do two passes: first compute per-group totals and counts; second append the group mean to each row.
Use dictionaries keyed by rows[i][group_key_idx]: sums[key] += value, counts[key] += 1, then mean = sums[key]/counts[key].
When building output, don’t mutate input rows: new_row = list(row) then new_row.append(means[key]), preserving original order.