Numpy:沿指定轴重塑数组

All*_*len 6 python arrays numpy

我有以下数组:

x = np.arange(24).reshape((2,3,2,2))
array([[[[ 0,  1],
     [ 2,  3]],

    [[ 4,  5],
     [ 6,  7]],

    [[ 8,  9],
     [10, 11]]],


   [[[12, 13],
     [14, 15]],

    [[16, 17],
     [18, 19]],

    [[20, 21],
     [22, 23]]]])
Run Code Online (Sandbox Code Playgroud)

我想将它重塑为一个 (3,4,2) 数组,如下所示:

array([[[ 0,  1],
    [ 2,  3],
    [12, 13],
    [14, 15]],

   [[ 4,  5],
    [ 6,  7],
    [16, 17],
    [18, 19]],

   [[ 8,  9],
    [10, 11],
    [20, 21],
    [22, 23]]])
Run Code Online (Sandbox Code Playgroud)

我尝试使用 reshape 但它给了我以下不是我想要的。

array([[[ 0,  1],
    [ 2,  3],
    [ 4,  5],
    [ 6,  7]],

   [[ 8,  9],
    [10, 11],
    [12, 13],
    [14, 15]],

   [[16, 17],
    [18, 19],
    [20, 21],
    [22, 23]]])
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

Div*_*kar 9

使用transpose然后reshape像这样 -

shp = x.shape
out = x.transpose(1,0,2,3).reshape(shp[1],-1,shp[-1])
Run Code Online (Sandbox Code Playgroud)