0

I have a 1D array that I need to reshape() into a 2D array, but the reshape is tricky because it seems like some kind of grouping is needed.

I have tried a few different series of reshape() and transpose() combinations, but I have not landed on the correct arrangement of values.

I have provided two arrays below. I would like to reshape the 1D array a to have the same shape as the 2D array b. What makes this trick is that I want the 4 ones in the middle. I made some attempts to make 2x2 blocks for every 4 values then arrange into the 6x6 array, but I did not have success.

a = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

b = np.array([
    [0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0],
    [0, 0, 1, 1, 0, 0],
    [0, 0, 1, 1, 0, 0],
    [0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0]])

Here are the shapes

> a.shape                                                                          
(36,)                                                                                
> b.shape                                                                          
(6, 6)   
1
  • 1
    That doesn't look like a reshape. in a the ones are contiguous and in b the ones are not contiguous. It seems like some shuffling of items will be needed but which items will get shuffled where. Do your real arrays look like that - ones and zeros? or is that an example? Commented Mar 13, 2021 at 20:05

1 Answer 1

2

This does it:

In [327]: a = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0,
     ...:  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
     ...: 
In [328]: a.reshape(3,3,2,2)
Out[328]: 
array([[[[0, 0],
         [0, 0]],

        [[0, 0],
         [0, 0]],

        [[0, 0],
         [0, 0]]],


       [[[0, 0],
         [0, 0]],

        [[1, 1],
         [1, 1]],

        [[0, 0],
         [0, 0]]],


       [[[0, 0],
         [0, 0]],

        [[0, 0],
         [0, 0]],

        [[0, 0],
         [0, 0]]]])

the (2,2) block of 1s is visible, but I need to switch the middle 2 dimensions:

In [329]: a.reshape(3,3,2,2).transpose(0,2,1,3)
Out[329]: 
array([[[[0, 0],
         [0, 0],
         [0, 0]],

        [[0, 0],
         [0, 0],
         [0, 0]]],


       [[[0, 0],
         [1, 1],
         [0, 0]],

        [[0, 0],
         [1, 1],
         [0, 0]]],


       [[[0, 0],
         [0, 0],
         [0, 0]],

        [[0, 0],
         [0, 0],
         [0, 0]]]])

now back to 2d:

In [330]: a.reshape(3,3,2,2).transpose(0,2,1,3).reshape(6,6)
Out[330]: 
array([[0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 1, 1, 0, 0],
       [0, 0, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0]])

The basic steps of reshape, transpose, followed by reshape is common to this kind of problem. The transpose details very.

Sign up to request clarification or add additional context in comments.

2 Comments

This is perfect, but I don't really understand the operation in the transpose. When I think transpose I think of flipping the rows and columns of a 2d matrix. Could you explain how you knew what to do with the transpose?
I wanted to keep the first and last dimensions the same (last more obviously). That left the middle 2 for switching.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.