This is an extremely simple question, but extensive search has not provided me with a satisfactory answer.
I have an array of numbers that evolve "over time" e.g. x = [1, 2, 3, 4, 5] and I want to calculate the mean at each timepoint. With a for-loop I would simply do
import numpy as np
x = [1, 2, 3, 4, 5]
y = np.empty(5)
for i in range(5):
y[i] = np.mean(x[0:i+1])
print(y)
[ 1. 1.5 2. 2.5 3. ]
In the processes I am working with, the numbers do not necessarily follow a simple dynamic like in the above. I wonder if there is some general way of applying a operation (such as calculating the mean) in a 'running' fashion, that is quicker than a for-loop?
numpyufuncshave anaccumulatemethod.