mean of elements i and i+1 in a numpy array
Out of curiosity, is there a specific numpy function to do the following (which would supposedly be faster):
a = np.array((开发者_Go百科0,2,4))
b = np.zeros(len(a) - 1)
for i in range(len(b)):
b[i] = a[i:i+2].mean()
print(b)
#prints [1,3]
Cheers
You could use
b = (a[1:] + a[:-1]) / 2.
to avoid the Python loop.
精彩评论