开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜