Compute a simple moving average of a 1-D NumPy array with np.convolve
Quick Overview
Implement a simple moving average over a 1-D NumPy signal by convolving it with a uniform kernel. It tests how np.convolve modes control the output length, how to validate the window size, and which faster approaches work for long arrays.
Compute a simple moving average of a 1-D NumPy array with np.convolve
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `moving_average(a: np.ndarray, w: int) -> np.ndarray` returning the length-`w` simple moving average of a 1-D signal. Each output element is the mean of a contiguous window of `w` samples.
Requirements:
- Return a 1-D `np.ndarray` of length `len(a) - w + 1`.
- Use `np.convolve(a, np.ones(w) / w, mode='valid')` (no explicit Python loops).
Example:
```python
a = np.array([1, 2, 3, 4, 5])
moving_average(a, 3)
# array([2., 3., 4.]) means of [1, 2, 3], [2, 3, 4] and [3, 4, 5]
```
```hint Count the windows
Work out how many complete windows of length `w` fit in a signal of length `n`. Then check what `np.convolve` returns when the kernel is longer than the signal.
```
### What a Strong Answer Covers
- Builds a uniform kernel whose weights sum to 1 and uses `'valid'` mode so the output has exactly `len(a) - w + 1` values
- Explains why convolving with that kernel gives the mean of each window, including why the kernel flip in convolution does not matter here
- Handles invalid inputs such as `w < 1`, `w > len(a)` and arrays that are not 1-D
- States the time cost of the convolution and names a faster approach for long signals
- Notes that the output is floating point and how that affects comparisons in tests
### Follow-up Questions
- How would you produce a same-length output aligned to the input?
- How would you compute this more efficiently for very large arrays?
- How would you compute a weighted moving average where newer samples count more, and what changes in the `np.convolve` call?
- How would you handle `NaN` values in the signal?
Overview: Implement a simple moving average over a 1-D NumPy signal by convolving it with a uniform kernel. It tests how np.convolve modes control the output length, how to validate the window size, and which faster approaches work for long arrays.
Implement moving_average(a: np.ndarray, w: int) -> np.ndarray returning the length-w simple moving average of a 1-D signal. Each output element is the mean of a contiguous window of w samples.
Requirements:
Return a 1-D
np.ndarray
of length
len(a) - w + 1
.
Use
np.convolve(a, np.ones(w) / w, mode='valid')
(no explicit Python loops).
Example:
a = np.array([1, 2, 3, 4, 5])
moving_average(a, 3)
# array([2., 3., 4.]) means of [1, 2, 3], [2, 3, 4] and [3, 4, 5]
What a Strong Answer Covers Guidance
Builds a uniform kernel whose weights sum to 1 and uses
'valid'
mode so the output has exactly
len(a) - w + 1
values
Explains why convolving with that kernel gives the mean of each window, including why the kernel flip in convolution does not matter here
Handles invalid inputs such as
w < 1
,
w > len(a)
and arrays that are not 1-D
States the time cost of the convolution and names a faster approach for long signals
Notes that the output is floating point and how that affects comparisons in tests
Follow-up Questions Guidance
How would you produce a same-length output aligned to the input?
How would you compute this more efficiently for very large arrays?
How would you compute a weighted moving average where newer samples count more, and what changes in the
np.convolve
call?