Using reshape in Python to reshape an array

耗尽温柔 提交于 2019-12-21 20:04:02

问题


I have an array that looks like below:

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

How can I use reshape to divide it into 4 chucks, such that it looks like

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

I tried different integer combinations of m, n, l in reshape(m,n,l), but none works.


回答1:


Edit: Sorry, I didn't realize it was a 3-d result, not a 4-d result. To get the 3-d one, you would have to reshape once more. And that extra reshape will copy the data.

You can't, you need to tranpose as well:

In [1]: a = np.arange(8)[:,None].repeat(8,axis=1)

In [2]: a
Out[2]: 
array([[0, 0, 0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3, 3, 3, 3],
       [4, 4, 4, 4, 4, 4, 4, 4],
       [5, 5, 5, 5, 5, 5, 5, 5],
       [6, 6, 6, 6, 6, 6, 6, 6],
       [7, 7, 7, 7, 7, 7, 7, 7]])

In [3]: b = a.reshape(2,4,2,4)

In [4]: b
Out[4]: 
array([[[[0, 0, 0, 0],
         [0, 0, 0, 0]],
         ...
        [[7, 7, 7, 7],
         [7, 7, 7, 7]]]])

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

        [[0, 0, 0, 0],
         [1, 1, 1, 1],
         [2, 2, 2, 2],
         [3, 3, 3, 3]]],


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

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



回答2:


Underneath all numpy arrays (and really, all arrays (not linked lists)) are linear chunks of memory, the higher dimensional nature is put on it by your interpretation. The way to think about it is element [i, j] is really element [i * num_cols + j] in the underlying array.

Numpy takes care of all the striding details for you to let you easily index into the memory using what ever dimentionality you want, however you have the constraint that you can only re-shape the data into arrays where you can write a rule like the above for converting (i,j) -> a single index, which what you want does not.

There are a bunch of ways you can do what you want, but they all involve copying data

In [6]: array([[0, 0, 0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3, 3, 3, 3],
       [4, 4, 4, 4, 4, 4, 4, 4],
       [5, 5, 5, 5, 5, 5, 5, 5],
       [6, 6, 6, 6, 6, 6, 6, 6],
       [7, 7, 7, 7, 7, 7, 7, 7]]).reshape(-1, 4)[np.r_[range(0, 8, 2), range(1, 8, 2), range(8, 16, 2), range(9, 16, 2)]].reshape(4, 4, 4)
Out[6]: 
array([[0, 0, 0, 0],
       [1, 1, 1, 1],
       [2, 2, 2, 2],
       [3, 3, 3, 3],
       [0, 0, 0, 0],
       [1, 1, 1, 1],
       [2, 2, 2, 2],
       [3, 3, 3, 3],
       [4, 4, 4, 4],
       [5, 5, 5, 5],
       [6, 6, 6, 6],
       [7, 7, 7, 7],
       [4, 4, 4, 4],
       [5, 5, 5, 5],
       [6, 6, 6, 6],
       [7, 7, 7, 7]])

or assuming your array is in a

In [10]: np.vstack([a[:4, :4], a[:4, 4:], a[4:, :4], a[4:, 4:]])
Out[10]: 
array([[0, 0, 0, 0],
       [1, 1, 1, 1],
       [2, 2, 2, 2],
       [3, 3, 3, 3],
       [0, 0, 0, 0],
       [1, 1, 1, 1],
       [2, 2, 2, 2],
       [3, 3, 3, 3],
       [4, 4, 4, 4],
       [5, 5, 5, 5],
       [6, 6, 6, 6],
       [7, 7, 7, 7],
       [4, 4, 4, 4],
       [5, 5, 5, 5],
       [6, 6, 6, 6],
       [7, 7, 7, 7]]).reshape(4, 4, 4)

or just

np.array([a[:4, :4], a[:4, 4:], a[4:, :4], a[4:, 4:]])


来源:https://stackoverflow.com/questions/19482527/using-reshape-in-python-to-reshape-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!