Using reshape in Python to reshape an array

2020-07-22 18:13发布

问题:

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:]])