22

I want to reshape the numpy array as it is depicted, from 3D to 2D. Unfortunately, the order is not correct.

A assume to have a numpy array (1024, 64, 100) and want to convert it to (1024*100, 64).

Does anybody has an idea how to maintain the order?

Image - click here

I have a sample data

data[0,0,0]=1
data[0,1,0]=2
data[0,2,0]=3
data[0,3,0]=4
data[1,0,0]=5
data[1,1,0]=6
data[1,2,0]=7
data[1,3,0]=8
data[2,0,0]=9
data[2,1,0]=10
data[2,2,0]=11
data[2,3,0]=12
data[0,0,1]=20
data[0,1,1]=21
data[0,2,1]=22
data[0,3,1]=23
data[1,0,1]=24
data[1,1,1]=25
data[1,2,1]=26
data[1,3,1]=27
data[2,0,1]=28
data[2,1,1]=29
data[2,2,1]=30
data[2,3,1]=31

and I would like to have an outcome like this:

array([[  1.,   2.,   3.,   4.],
       [  5.,   6.,   7.,   8.],
       [  9.,  10.,  11.,  12.],
       [ 20.,  21.,  22.,  23.],
       [ 24.,  25.,  26.,  27.],
       [ 28.,  29.,  30.,  31.]])

Moreover, I would also like to have the reshaping in the other way, i.e. from:

array([[  1.,   2.,   3.,   4.],
       [  5.,   6.,   7.,   8.],
       [  9.,  10.,  11.,  12.],
       [ 20.,  21.,  22.,  23.],
       [ 24.,  25.,  26.,  27.],
       [ 28.,  29.,  30.,  31.]])

to the desired output:

 [[[  1.  20.]
  [  2.  21.]
  [  3.  22.]
  [  4.  23.]]

 [[  5.  24.]
  [  6.  25.]
  [  7.  26.]
  [  8.  27.]]

 [[  9.  28.]
  [ 10.  29.]
  [ 11.  30.]
  [ 12.  31.]]]
0

2 Answers 2

30

It looks like you can use numpy.transpose and then reshape, like so -

data.transpose(2,0,1).reshape(-1,data.shape[1])

Sample run -

In [63]: data
Out[63]: 
array([[[  1.,  20.],
        [  2.,  21.],
        [  3.,  22.],
        [  4.,  23.]],

       [[  5.,  24.],
        [  6.,  25.],
        [  7.,  26.],
        [  8.,  27.]],

       [[  9.,  28.],
        [ 10.,  29.],
        [ 11.,  30.],
        [ 12.,  31.]]])

In [64]: data.shape
Out[64]: (3, 4, 2)

In [65]: data.transpose(2,0,1).reshape(-1,data.shape[1])
Out[65]: 
array([[  1.,   2.,   3.,   4.],
       [  5.,   6.,   7.,   8.],
       [  9.,  10.,  11.,  12.],
       [ 20.,  21.,  22.,  23.],
       [ 24.,  25.,  26.,  27.],
       [ 28.,  29.,  30.,  31.]])

In [66]: data.transpose(2,0,1).reshape(-1,data.shape[1]).shape
Out[66]: (6, 4)

To get back original 3D array, use reshape and then numpy.transpose, like so -

In [70]: data2D.reshape(np.roll(data.shape,1)).transpose(1,2,0)
Out[70]: 
array([[[  1.,  20.],
        [  2.,  21.],
        [  3.,  22.],
        [  4.,  23.]],

       [[  5.,  24.],
        [  6.,  25.],
        [  7.,  26.],
        [  8.,  27.]],

       [[  9.,  28.],
        [ 10.,  29.],
        [ 11.,  30.],
        [ 12.,  31.]]])
Sign up to request clarification or add additional context in comments.

2 Comments

Just found this question by accident, saw the first line of the answer, and guessed the author;) Thanks for the tip!
@AndrasDeak You wandering around looking for old NumPy questions because you are too bored? :)
3

Using einops:

# start with (1024, 64, 100) to (1024*100, 64):
einops.rearrange('h w i -> (i h) w')

# or we could concatenate along horizontal axis to get (1024, 64 * 100):
einops.rearrange('h w i -> h (i w)')

See docs for more examples

Comments

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.