0

I have an array 'aN' with a shape equal to (1000,151). I need to calculate the average every 10 data in rows, so I implemented this

arr = aN[:]
window_size = 10
i = 0
moving_averages = []

while i < len(arr) - window_size + 1:
    window_average = round(np.sum(arr[i:i+window_size]) / window_size, 2)      
    moving_averages.append(window_average)
    i += 10

The point is that my output is a list of 100 data, but I need an array with the same number of columns that the original array (151). Any idea on how to get this outcome??

TIA!!

Pahan
  • 5
  • 2
  • Does this answer your question? [How to calculate rolling / moving average using python + NumPy / SciPy?](https://stackoverflow.com/questions/14313510/how-to-calculate-rolling-moving-average-using-python-numpy-scipy) – Dominik Stańczak Jul 06 '22 at 13:18

1 Answers1

0

If you convert it to a pandas dataframe, you can use the rolling() function of pandas together with the mean() function. It should be able to accomplish what you need.

jcnc
  • 90
  • 6
  • Hi :) Thanks, but I think the difficult point here is that aN is 2-dimensional. Even though I am calculating the moving average just for rows, I don't find a proper way to get the output what I need :( – Pahan Jul 06 '22 at 13:32
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 06 '22 at 15:42
  • This finally worked :) The missing step was to convert to pandas dataframe and then back to numpy. Many thanks! – Pahan Jul 08 '22 at 07:11