Moving Average

mediumnumpy_reductions

Implement moving_average(a, n).

Given a 1D array a and a window length n, return the array of length-n moving averages. The output has length len(a) - n + 1, where output i is the mean of a[i : i + n]. Vectorize it — no for/while loops (hint: prefix sums).

Constraints
  • no scipy, sklearn
  • no for/while loops

Your solution

Edit moving_average and run the real pytest suite in your browser — no install required. Your code is saved locally.

⌘/Ctrl + ↵

Loading the Python runtime (first run only)…

Hints

Hint 1

np.cumsum gives prefix sums; a window sum is the difference of two of them.