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

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

    Can always use np.einsum:

    >>> a = np.arange(11*5*5).reshape(11,5,5)
    >>> np.einsum('...ijk->...i',a)/(a.shape[-1]*a.shape[-2])
    array([ 12,  37,  62,  87, 112, 137, 162, 187, 212, 237, 262])
    

    Works on higher dimensional arrays (all of these methods would if the axis labels are changed):

    >>> a = np.arange(10*11*5*5).reshape(10,11,5,5)
    >>> (np.einsum('...ijk->...i',a)/(a.shape[-1]*a.shape[-2])).shape
    (10, 11)
    

    Faster to boot:

    a = np.arange(11*5*5).reshape(11,5,5)
    
    %timeit a.reshape(11, 25).mean(axis=1)
    10000 loops, best of 3: 21.4 us per loop
    
    %timeit a.mean(axis=(1,2))
    10000 loops, best of 3: 19.4 us per loop
    
    %timeit np.einsum('...ijk->...i',a)/(a.shape[-1]*a.shape[-2])
    100000 loops, best of 3: 8.26 us per loop
    

    Scales slightly better then the other methods as array size increases.

    Using dtype=np.float64 does not change the above timings appreciably, so just to double check:

    a = np.arange(110*50*50,dtype=np.float64).reshape(110,50,50)
    
    %timeit a.reshape(110,2500).mean(axis=1)
    1000 loops, best of 3: 307 us per loop
    
    %timeit a.mean(axis=(1,2))
    1000 loops, best of 3: 308 us per loop
    
    %timeit np.einsum('...ijk->...i',a)/(a.shape[-1]*a.shape[-2])
    10000 loops, best of 3: 145 us per loop
    

    Also something that is interesting:

    %timeit np.sum(a) #37812362500.0
    100000 loops, best of 3: 293 us per loop
    
    %timeit np.einsum('ijk->',a) #37812362500.0
    100000 loops, best of 3: 144 us per loop
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 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+.

    0 讨论(0)
提交回复
热议问题