整形的一點是,所有你需要能夠sum
在軸:
import numpy
M = numpy.arange(100).reshape(5, 20)
M[:, :4]
#>>> array([[ 0, 1, 2, 3],
#>>> [20, 21, 22, 23],
#>>> [40, 41, 42, 43],
#>>> [60, 61, 62, 63],
#>>> [80, 81, 82, 83]])
M[:, 4:8]
#>>> array([[ 4, 5, 6, 7],
#>>> [24, 25, 26, 27],
#>>> [44, 45, 46, 47],
#>>> [64, 65, 66, 67],
#>>> [84, 85, 86, 87]])
...
M[:, 16:20]
#>>> array([[16, 17, 18, 19],
#>>> [36, 37, 38, 39],
#>>> [56, 57, 58, 59],
#>>> [76, 77, 78, 79],
#>>> [96, 97, 98, 99]])
M.reshape(M.shape[0], -1, 4).sum(axis=1)
#>>> array([[ 40, 45, 50, 55],
#>>> [140, 145, 150, 155],
#>>> [240, 245, 250, 255],
#>>> [340, 345, 350, 355],
#>>> [440, 445, 450, 455]])