Is there a way to vectorize a function so that the output would be an array of means where each mean represents the mean of the values from 0-index of the input array? Looping this is pretty straightforward but I am trying to be as efficient as possible. e.g. 0 = mean(0), 1 = mean(0-1), N = mean(0-N)
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Extract matrix elements using a vector of column i
- Evil ctypes hack in python
The intended operation could be coined as
cumulative averaging
. So, an obvious solution would involvecumulative summation
and dividing those summations by the number of elements participating for each such summation. Thus, a vectorized implementation would involvenp.cumsum
and then dividing by the number of participating elements that could be obtained withnp.arange
and generalized for an ndarray, like so -If you're able to use
pandas
there isexpanding_mean
which will work directly with a NumPy array:This method also works column-wise: