Get mean of 2D slice of a 3D array in numpy

前端 未结 3 1831
离开以前
离开以前 2020-12-16 14:12

I have a numpy array with a shape of:

(11L, 5L, 5L)

I want to calculate the mean over the 25 elements of each \'slice\' of the array [0, :,

3条回答
  •  清歌不尽
    2020-12-16 14:45

    You can reshape(11, 25) and then call mean only once (faster):

    a.reshape(11, 25).mean(axis=1)
    

    Alternatively, you can call np.mean twice (about 2X slower on my computer):

    a.mean(axis=2).mean(axis=1)
    

提交回复
热议问题