这是一个可能的解决方案。
import numpy as np
import itertools
>>> x = np.arange(16).reshape((4,4))
>>> x
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
>>> for i, j in itertools.product([0,2],[0,2]):
... print np.sum(x[i:i+2,j:j+2])
...
10
18
42
50
>>>
找到了一个优雅的解决方案here https://stackoverflow.com/questions/36383107/how-to-evaluate-the-sum-of-values-within-array-blocks.
>>> x.reshape(2,2,2,2).sum(axis=(1,3))
array([[10, 18],
[42, 50]])
>>>