Numpy:在矩阵块数组中重塑2D数组

时间:2017-11-22 20:20:55

标签: python arrays numpy multidimensional-array reshape

我想在一个3D阵列中重塑一个2D numpy数组,该数组由2x2(或一般PxP)块组成,尊重空间位置。

x = np.arange(100).reshape(10,10)
x
array([[ 0,  1,  2, ...,  7,  8,  9],
       [10, 11, 12, ..., 17, 18, 19],
       [20, 21, 22, ..., 27, 28, 29],
       ..., 
       [70, 71, 72, ..., 77, 78, 79],
       [80, 81, 82, ..., 87, 88, 89],
       [90, 91, 92, ..., 97, 98, 99]])

y = some_reshape_and_transpose(x)
y[0]
array([[ 0,  1],
       [10, 11]])
y[1]
array([[ 2,  3],
       [12, 13]])
y.shape
(25, 2, 2)

沿着3D张量的第一维的块的精确顺序并不重要。反向命令也将受到赞赏!

0 个答案:

没有答案