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

前端 未结 3 1842
离开以前
离开以前 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:56

    Use a tuple for axis :

    >>> a = np.arange(11*5*5).reshape(11,5,5)
    >>> a.mean(axis=(1,2))
    array([  12.,   37.,   62.,   87.,  112.,  137.,  162.,  187.,  212.,
            237.,  262.])
    

    Edit: This works only with numpy version 1.7+.

提交回复
热议问题