pythonarraysnumpymean

Mean over two consecutive elements of array


I would like to compute the mean of two consecutive elements of a python array, such that the length of the final array has the length equal to that of the original array minus one (so something like np.diff, but with the mean instead of the difference).

So if I have an array

a = [1, 2, 3, 4, 5, 6]

the output I would like to have would be

a_mean = [1.5, 2.5, 3.5, 4.5, 5.5]

Is there any smarter solution using numpy rather than looping? I could not come up with a smart solution.


Solution

  • np.convolve([1, 2, 3, 4, 5, 6], [.5, .5], mode='valid')