你可以这样做:
print arr.reshape(2,2,2,2).swapaxes(1,2).reshape(2,2,4).max(axis=-1)[[ 0.439 0.962] [-0.038 0.476]]
首先说明:
arr=np.array([[0.393,-0.428,-0.546,0.103],[0.439,-0.154,0.962,0.37,],[-0.038,-0.216,-0.314,0.458],[-0.123,-0.881,-0.204,0.476]])
我们首先要将轴分组为相关部分。
tmp = arr.reshape(2,2,2,2).swapaxes(1,2)print tmp[[[[ 0.393 -0.428] [ 0.439 -0.154]] [[-0.546 0.103] [ 0.962 0.37 ]]] [[[-0.038 -0.216] [-0.123 -0.881]] [[-0.314 0.458] [-0.204 0.476]]]]
再次重塑以获得所需的数据组:
tmp = tmp.reshape(2,2,4)print tmp[[[ 0.393 -0.428 0.439 -0.154] [-0.546 0.103 0.962 0.37 ]] [[-0.038 -0.216 -0.123 -0.881] [-0.314 0.458 -0.204 0.476]]]
最后沿最后一个轴取最大值。
对于平方矩阵,可以将其概括为:
k = arr.shape[0]/2arr.reshape(k,2,k,2).swapaxes(1,2).reshape(k,k,4).max(axis=-1)
根据Jamie和Dougal的评论,我们可以进一步概括一下:
n = 2 #Height of windowm = 2 #Width of windowk = arr.shape[0] / n #Must divide evenlyl = arr.shape[1] / m #Must divide evenlyarr.reshape(k,n,l,m).max(axis=(-1,-3)) #Numpy >= 1.7.1arr.reshape(k,n,l,m).max(axis=-3).max(axis=-1) #Numpy < 1.7.1



