Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

209. Mean aggregation

easy
GeneralGeneral
senior

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.

Requirements

Implement the function

python

Rules:

  • Compute the mean for each group (g) as ( \mu_g = \frac{1}{|g|}\sum_{i \in g} x_i ).
  • Append the corresponding group mean as a new last column to every row.
  • Don’t use pandas; use only NumPy and built-in Python libraries.
  • Keep the original row order unchanged.
  • Output must be a NumPy array.

Example

python

Output:

python
Input Signature
ArgumentType
rowsnp.ndarray
value_idxint
group_key_idxint
Output Signature
Return NameType
valuenp.ndarray

Constraints

  • No pandas; only Python/NumPy allowed

  • Return NumPy array

  • Preserve original row order

Hint 1

Do two passes: first compute per-group totals and counts; second append the group mean to each row.

Hint 2

Use dictionaries keyed by rows[i][group_key_idx]: sums[key] += value, counts[key] += 1, then mean = sums[key]/counts[key].

Hint 3

When building output, don’t mutate input rows: new_row = list(row) then new_row.append(means[key]), preserving original order.

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
senior
entry
Tags
groupby-aggregation
hash-map
feature-engineering
data-processing
26 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit