在NumPy中将4D数组重塑为2D数组的直觉和想法 [英] Intuition and idea behind reshaping 4D array to 2D array in NumPy

查看:59
本文介绍了在NumPy中将4D数组重塑为2D数组的直觉和想法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于教学原因,实施 Kronecker-product 时(没有使用明显且容易获得的np.kron()),我获得了一个4维数组作为中间结果,我必须对其进行重塑以获得最终结果.

While implementing a Kronecker-product for pedagogical reasons (without using the obvious and readily available np.kron()), I obtained a 4 dimensional array as an intermediate result, which I've to reshape to get the final result.

但是,我仍然不能全神贯注地重塑这些高维数组.我有这个4D数组:

But, I still can't wrap my head around reshaping these high dimensional arrays. I have this 4D array:

array([[[[ 0,  0],
         [ 0,  0]],

        [[ 5, 10],
         [15, 20]]],


       [[[ 6, 12],
         [18, 24]],

        [[ 7, 14],
         [21, 28]]]])

这是形状(2, 2, 2, 2),我想将其重塑为(4,4).可能有人认为

This is of shape (2, 2, 2, 2) and I'd like to reshape it to (4,4). One might think that this is obvious to do with

np.reshape(my4darr, (4,4))

但是,上面的重塑没有给了我预期的结果,即:

But, the above reshape does not give me the expected result which is:

array([[ 0,  5,  0, 10],
       [ 6,  7, 12, 14],
       [ 0, 15,  0, 20],
       [18, 21, 24, 28]])

如您所见,预期结果中的所有元素都存在于4D数组中.我只是无法根据需要正确地进行 reshape .除了答案之外,对如何对此类高维数组进行reshape的一些解释将非常有帮助.谢谢!

As you can see, all the elements in the expected result are present in the 4D array. I just can't get the hang of doing the reshape correctly as needed. In addition to the answer, some explanation of how to do the reshape for such high dimensional arrays would be really helpful. Thanks!

推荐答案

ndnd转换的一般想法

ndnd转换的想法仅使用两件事-

General idea for nd to nd transformation

The idea with such nd to nd transformation is using just two things -

  • Permute axes (with numpy.transpose or numpy.moveaxis or numpy.rollaxis if the needed permute order is a rolled one or numpy.swapaxes if just two axes need to be swapped) and

重塑.

置换轴::获得使拼合后的版本与输出的拼合后的版本相对应的顺序.因此,如果您最终以某种方式使用了它两次,请再次查看,因为您不应该这样做.

Permute axes : To get the order such that the flattened version corresponds to the flattened version of output. So, if you somehow end up using it twice, look again because you shouldn't.

重塑::拆分轴或将最终输出呈现所需的形状.当输入的输入信号较暗时,主要在开始时就需要分割轴,并且我们需要分割成多个块.同样,您不需要两次以上.

Reshape : To split the axes or bring the final output to the desired shape. Splitting axes is needed mostly at the start, when the input is of lower-dim and we are needed to split into blocks. Again, you shouldn't need this more than twice.

因此,通常我们将分三个步骤:

Hence, generally we would have three steps :

    [ Reshape ]      --->  [ Permute axes ]   --->  [ Reshape ]

 Create more axes             Bring axes             Merge axes
                          into correct order

回溯方法

给定输入和输出通过的最安全的解决方法是所谓的回溯方法,即,将输入的轴分开(当从较小的nd变为较大的nd时),或者分割输出的轴(当从较大的nd变为较小的nd时).拆分的想法是使较小的nd的暗号数量与较大的nd的暗号数量相同.然后,研究输出的步幅并将其与输入进行匹配,以获得所需的置换顺序.最后,如果最后一个是较小的nd,则可能需要在最后进行重塑(默认方式或C顺序)以合并轴.

The safest way to solve, given the input and output is through, what one could call as the back-tracking method, i.e. split the axes of the input (when going from smaller nd to bigger nd) or split the axes of the output (when going from bigger nd to smaller nd). The idea with the splitting is to bring the number of dims of the smaller nd one same as the bigger nd one. Then, study the strides of the output and match it up against the input to get the required permute order. Finally, a reshape (default way or C order) might be needed at the end, if the final one is a smaller nd one, to merge axes.

如果输入和输出的暗淡次数相同,那么我们将需要将它们拆分成小块并研究它们彼此之间的跨度.在这种情况下,我们应该具有块大小的附加输入参数,但这可能是题外话.

If both input and output are of same number of dims, then we would need to split both and break into blocks and study their strides against each other. In such cases, we should have the additional input parameter of block sizes, but that's probably off-topic.

让我们使用此特定案例来演示如何应用这些策略.在这里,输入为4D,而输出为2D.因此,最有可能的是,我们不需要重塑就可以拆分.因此,我们需要从置换轴开始.由于最终输出不是4D,而是2D,因此我们需要在最后进行重塑.

Let's use this specific case to demonstrate how to apply those strategies. In here, the input is 4D, while output is 2D. So, most probably, we won't need reshape to split. So, we need to start with permuting axes. Since, the final output is not 4D, but a 2D one, we would need a reshape at the end.

现在,这里的输入是:

In [270]: a
Out[270]: 
array([[[[ 0,  0],
         [ 0,  0]],

        [[ 5, 10],
         [15, 20]]],


       [[[ 6, 12],
         [18, 24]],

        [[ 7, 14],
         [21, 28]]]])

预期输出为:

In [271]: out
    Out[271]: 
    array([[ 0,  5,  0, 10],
           [ 6,  7, 12, 14],
           [ 0, 15,  0, 20],
           [18, 21, 24, 28]])

此外,这是从较大的nd到较小的nd转换,因此回溯方法将涉及到拆分输出并研究其

Also, this is a bigger nd to smaller nd transformation, so the back-tracking method would involve, splitting the output and studying its strides and matching up against the corresponding values in input :

                    axis = 3
                   ---      -->          

                    axis = 1                    
                   ------>           
axis=2|  axis=0|   [ 0,  5,  0, 10],        

               |   [ 6,  7, 12, 14],
               v  
      |            [ 0, 15,  0, 20],
      v
                   [18, 21, 24, 28]])

因此,所需的排列顺序是(2,0,3,1):

Hence, the permuted order needed is (2,0,3,1) :

In [275]: a.transpose((2, 0, 3, 1))
Out[275]: 
array([[[[ 0,  5],
         [ 0, 10]],

        [[ 6,  7],
         [12, 14]]],


       [[[ 0, 15],
         [ 0, 20]],

        [[18, 21],
         [24, 28]]]])

然后,只需将其重塑为所需形状:

Then, simply reshape to the expected shape :

In [276]: a.transpose((2, 0, 3, 1)).reshape(4,4)
Out[276]: 
array([[ 0,  5,  0, 10],
       [ 6,  7, 12, 14],
       [ 0, 15,  0, 20],
       [18, 21, 24, 28]])


更多示例

我挖掘了自己的历史,并发现根据ndnd转换的Q&As很少.这些可以作为其他示例案例,尽管解释较少(大多数情况下).如前所述,最多两个reshapes和最多一个swapaxes/transpose到处都有工作.它们在下面列出:

I dug up my history and found few Q&As based on nd to nd transformations. These could serve as other example cases, albeit with lesser explanation (mostly). As mentioned earlier, at most two reshapes and at most one swapaxes/transpose did the job everywhere. They are listed below :

  • Python Reshape 3d array into 2d
  • reshape an array using python/numpy
  • Merging non-overlapping array blocks
  • Conversion from a Numpy 3D array to a 2D array
  • how to reshape an N length vector to a 3x(N/3) matrix in numpy using reshape
  • Construct image from 4D list
  • Reshaping/Combining several sub-matrices to one matrix in multi-dimensional space
  • Interlace various small 2D matrices into a bigger one
  • how to retrieve every section by 3X3?
  • Reshaping 3D Numpy Array to a 2D array
  • Iterate in submatrices through a bigger matrix
  • Reorganizing a 2D numpy array into 3D
  • Numpy change shape from (3, 512, 660, 4) to (3,2048,660,1)
  • Numpy: rotate sub matrix m of M
  • Split a 3D numpy array into 3D blocks
  • Converting 3D matrix to cascaded 2D Matrices
  • Rearranging numpy array
  • Numpy: Reshape array along a specified axis
  • How to construct 2d array from 2d arrays
  • How to form a matrix from submatrices?
  • Python: Reshape 3D image series to pixel series

这篇关于在NumPy中将4D数组重塑为2D数组的直觉和想法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆