繁体   English   中英

使用zip将2 numpy尺寸重塑为一个吗?

[英]Reshaping 2 numpy dimensions into one with zipping?

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

描述问题的最快方法是一个简单的示例:可以说我有一个2D numpy arrayl,如下所示:

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

因此它的形状为[3,6],我想将其重塑为一个1D数组,如下所示:

[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, ..................],
 [...........................................]]

编辑:仅仅为了谷歌搜索此问题的人,我添加了一些人可能会搜索的搜索词:

交织维数numpy数组

邮编尺寸numpy数组

整形整齐的尺寸

张量重塑尺寸时间步长

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

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

我们需要用np.swapaxesnp.transpose交换最后两个轴,然后重塑np.transpose

对于2D输入情况,它将是-

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

对于3D输入情况,只有重塑零件会发生变化-

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

通用方式:要使其通用,可以涵盖所有n-dim阵列情况-

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

样品运行

2D外壳:

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

另外,您可以将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() ,您无需在此处指定任何形状信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM