假设我有以下数组:
a = np.array([[1,2,3,4,5,6],
[7,8,9,10,11,12],
[3,5,6,7,8,9]])
Run Code Online (Sandbox Code Playgroud)
我想总结第一行的前两个值:1+2 = 3,然后接下来的两个值:3+4 = 7,然后5+6 = 11,依此类推每一行.我想要的输出是这样的:
array([[ 3, 7, 11],
[15, 19, 23],
[ 8, 13, 17]])
Run Code Online (Sandbox Code Playgroud)
我有以下解决方案:
def sum_chunks(x, chunk_size):
rows, cols = x.shape
x = x.reshape(x.size / chunk_size, chunk_size)
return x.sum(axis=1).reshape(rows, cols/chunk_size)
Run Code Online (Sandbox Code Playgroud)
但感觉不必要的复杂,有更好的方法吗?也许内置?