Aggregate Numpy Array By Summing

前端 未结 3 1224
耶瑟儿~
耶瑟儿~ 2021-01-21 01:37

I have a Numpy array of shape (4320,8640). I would like to have an array of shape (2160,4320).

You\'ll notice that each cell of the new array m

3条回答
  •  星月不相逢
    2021-01-21 02:34

    When the window fits exactly into the array, reshaping to more dimensions and collapsing the extra dimensions with np.sum is sort of the canonical way of doing this with numpy:

    >>> a = np.random.rand(4320,8640)
    >>> a.shape
    (4320, 8640)
    >>> a_small = a.reshape(2160, 2, 4320, 2).sum(axis=(1, 3))
    >>> a_small.shape
    (2160, 4320)
    >>> np.allclose(a_small[100, 203], a[200:202, 406:408].sum())
    True
    

提交回复
热议问题