通过压缩将 2 个 numpy 维度重塑为一个维度?

Reshaping 2 numpy dimensions into one with zipping?

我尝试搜索这个问题,但找不到任何相关内容。

描述问题的最快方法是举一个简单的例子: 假设我有一个像这样的 2D numpy 数组:

[[0, 1, 2, 3],
[10, 11, 12, 13],
[20, 21, 22, 23]]

所以它的形状是 [3,6] 我想将其重塑为如下所示的一维数组:

[0, 10 ,20 ,1 ,11 ,21 ,2 ,12 ,22 ,3 ,13 ,23 ]

不同于我们通过整形得到的数组:

[ 0,  1,  2,  3, 10, 11, 12, 13, 20, 21, 22, 23]

现在,对于我遇到的实际问题... 我实际上有一个 3D 数组,我想将它重塑为 2D 数组,我想用上面描述的方法这样做。 另一个例子是:

import numpy
a = numpy.array([[[0,1,2],[10,11,12],[20,21,22]],
[[100,101,102],[110,111,112],[120,121,122]],
 [[200,201,202],[210,211,212],[220,221,222]]])
a.shape
a.reshape(3,9)
OUTPUT: array([[  0,   1,   2,  10,  11,  12,  20,  21,  22],
       [100, 101, 102, 110, 111, 112, 120, 121, 122],
       [200, 201, 202, 210, 211, 212, 220, 221, 222]])

再一次,我希望我的输出看起来像这样:

[[  0,  10,  20,   1,  11,  21,   2,  12,  22],
 [100, 110, 120, 101, 111, ..................],
 [...........................................]]

编辑:为了 google 这个问题的人,我添加了一些搜索词,有些人可能会搜索:

交织维度 numpy 数组

Zip 维度 numpy 数组

Numpy 重塑有序维度

张量重塑维度时间步长

我们需要用 np.swapaxesnp.transpose 交换最后两个轴,然后重新整形。

对于2D输入大小写,应该是-

a.swapaxes(-2,-1).ravel()

对于3D input case,只有reshape部分改变-

a.swapaxes(-2,-1).reshape(a.shape[0],-1)

通用方式:使其通用,涵盖所有 n-dim 数组情况 -

a.swapaxes(-2,-1).reshape(a.shape[:-2] + (-1,)).squeeze()

样本运行

二维案例:

In [186]: a
Out[186]: 
array([[ 0,  1,  2,  3],
       [10, 11, 12, 13],
       [20, 21, 22, 23]])

In [187]: a.swapaxes(-2,-1).reshape(a.shape[:-2] + (-1,)).squeeze()
Out[187]: array([ 0, 10, 20,  1, 11, 21,  2, 12, 22,  3, 13, 23])

3D 案例:

In [189]: a
Out[189]: 
array([[[  0,   1,   2],
        [ 10,  11,  12],
        [ 20,  21,  22]],

       [[100, 101, 102],
        [110, 111, 112],
        [120, 121, 122]],

       [[200, 201, 202],
        [210, 211, 212],
        [220, 221, 222]]])

In [190]: a.swapaxes(-2,-1).reshape(a.shape[:-2] + (-1,)).squeeze()
Out[190]: 
array([[  0,  10,  20,   1,  11,  21,   2,  12,  22],
       [100, 110, 120, 101, 111, 121, 102, 112, 122],
       [200, 210, 220, 201, 211, 221, 202, 212, 222]])

运行时测试-

In [14]: a = np.random.rand(3,3,3)

# @mahdi n75's soln
In [15]: %timeit np.reshape(a,(3,9), order='F')
1000000 loops, best of 3: 1.22 µs per loop

In [16]: %timeit a.swapaxes(-2,-1).reshape(a.shape[0],-1)
1000000 loops, best of 3: 1.01 µs per loop

In [20]: a = np.random.rand(30,30,30)

# @mahdi n75's soln
In [21]: %timeit np.reshape(a,(30,900), order='F')
10000 loops, best of 3: 28.4 µs per loop

In [22]: %timeit a.swapaxes(-2,-1).reshape(a.shape[0],-1)
10000 loops, best of 3: 18.9 µs per loop

In [17]: a = np.random.rand(300,300,300)

# @mahdi n75's soln
In [18]: %timeit np.reshape(a,(300,90000), order='F')
1 loop, best of 3: 333 ms per loop

In [19]: %timeit a.swapaxes(-2,-1).reshape(a.shape[0],-1)
10 loops, best of 3: 52.4 ms per loop

numpy

的文档中有一个简单的答案

np.reshape(a,(3,9), order='F')

此外,您可以将 np.ravelorder='F' 一起使用。

In [35]: arr
Out[35]: 
array([[ 0,  1,  2,  3],
       [10, 11, 12, 13],
       [20, 21, 22, 23]])

In [36]: np.ravel(arr, order='F')
Out[36]: array([ 0, 10, 20,  1, 11, 21,  2, 12, 22,  3, 13, 23])

请注意,与 np.reshape() 不同,您不必在此处指定任何形状信息。