Discussion:
[Numpy-discussion] reshaping array question
Neal Becker
2015-11-17 15:48:44 UTC
Permalink
I have an array of shape
(7, 24, 2, 1024)

I'd like an array of
(7, 24, 2048)

such that the elements on the last dimension are interleaving the elements
from the 3rd dimension

[0,0,0,0] -> [0,0,0]
[0,0,1,0] -> [0,0,1]
[0,0,0,1] -> [0,0,2]
[0,0,1,1] -> [0,0,3]
...

What might be the simplest way to do this?

------------
A different question, suppose I just want to stack them

[0,0,0,0] -> [0,0,0]
[0,0,0,1] -> [0,0,1]
[0,0,0,2] -> [0,0,2]
...
[0,0,1,0] -> [0,0,1024]
[0,0,1,1] -> [0,0,1025]
[0,0,1,2] -> [0,0,1026]
...
Robert Kern
2015-11-17 16:20:25 UTC
Permalink
Post by Neal Becker
I have an array of shape
(7, 24, 2, 1024)
I'd like an array of
(7, 24, 2048)
such that the elements on the last dimension are interleaving the elements
from the 3rd dimension
[0,0,0,0] -> [0,0,0]
[0,0,1,0] -> [0,0,1]
[0,0,0,1] -> [0,0,2]
[0,0,1,1] -> [0,0,3]
...
What might be the simplest way to do this?
np.transpose(A, (-2, -1)).reshape(A.shape[:-2] + (-1,))
Post by Neal Becker
------------
A different question, suppose I just want to stack them
[0,0,0,0] -> [0,0,0]
[0,0,0,1] -> [0,0,1]
[0,0,0,2] -> [0,0,2]
...
[0,0,1,0] -> [0,0,1024]
[0,0,1,1] -> [0,0,1025]
[0,0,1,2] -> [0,0,1026]
...
A.reshape(A.shape[:-2] + (-1,))

--
Robert Kern
Neal Becker
2015-11-17 18:49:21 UTC
Permalink
Post by Robert Kern
Post by Neal Becker
I have an array of shape
(7, 24, 2, 1024)
I'd like an array of
(7, 24, 2048)
such that the elements on the last dimension are interleaving the
elements from the 3rd dimension
[0,0,0,0] -> [0,0,0]
[0,0,1,0] -> [0,0,1]
[0,0,0,1] -> [0,0,2]
[0,0,1,1] -> [0,0,3]
...
What might be the simplest way to do this?
np.transpose(A, (-2, -1)).reshape(A.shape[:-2] + (-1,))
I get an error on that 1st transpose:

here, 'A' is 'fftouts'

print (fftouts.shape)
print (np.transpose (fftouts, (-2,-1)).shape)

(4, 24, 2, 1024) <<< fftouts.shape prints this
Traceback (most recent call last):
File "test_uw2.py", line 194, in <module>
run_line (sys.argv)
File "test_uw2.py", line 190, in run_line
run (opt)
File "test_uw2.py", line 103, in run
print (np.transpose (fftouts, (-2,-1)).shape)
File "/home/nbecker/.local/lib/python2.7/site-
packages/numpy/core/fromnumeric.py", line 551, in transpose
return transpose(axes)
ValueError: axes don't match array
Post by Robert Kern
Post by Neal Becker
------------
A different question, suppose I just want to stack them
[0,0,0,0] -> [0,0,0]
[0,0,0,1] -> [0,0,1]
[0,0,0,2] -> [0,0,2]
...
[0,0,1,0] -> [0,0,1024]
[0,0,1,1] -> [0,0,1025]
[0,0,1,2] -> [0,0,1026]
...
A.reshape(A.shape[:-2] + (-1,))
--
Robert Kern
Sebastian Berg
2015-11-17 18:53:34 UTC
Permalink
Post by Neal Becker
Post by Robert Kern
Post by Neal Becker
I have an array of shape
(7, 24, 2, 1024)
I'd like an array of
(7, 24, 2048)
such that the elements on the last dimension are interleaving the
elements from the 3rd dimension
[0,0,0,0] -> [0,0,0]
[0,0,1,0] -> [0,0,1]
[0,0,0,1] -> [0,0,2]
[0,0,1,1] -> [0,0,3]
...
What might be the simplest way to do this?
np.transpose(A, (-2, -1)).reshape(A.shape[:-2] + (-1,))
Transpose needs a slightly different input. If you look at the help, it
should be clear. The help might also point to np.swapaxes, which may be
a bit more straight forward for this exact case.
Post by Neal Becker
here, 'A' is 'fftouts'
print (fftouts.shape)
print (np.transpose (fftouts, (-2,-1)).shape)
(4, 24, 2, 1024) <<< fftouts.shape prints this
File "test_uw2.py", line 194, in <module>
run_line (sys.argv)
File "test_uw2.py", line 190, in run_line
run (opt)
File "test_uw2.py", line 103, in run
print (np.transpose (fftouts, (-2,-1)).shape)
File "/home/nbecker/.local/lib/python2.7/site-
packages/numpy/core/fromnumeric.py", line 551, in transpose
return transpose(axes)
ValueError: axes don't match array
Post by Robert Kern
Post by Neal Becker
------------
A different question, suppose I just want to stack them
[0,0,0,0] -> [0,0,0]
[0,0,0,1] -> [0,0,1]
[0,0,0,2] -> [0,0,2]
...
[0,0,1,0] -> [0,0,1024]
[0,0,1,1] -> [0,0,1025]
[0,0,1,2] -> [0,0,1026]
...
A.reshape(A.shape[:-2] + (-1,))
--
Robert Kern
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Robert Kern
2015-11-17 19:05:09 UTC
Permalink
Post by Sebastian Berg
Post by Robert Kern
Post by Neal Becker
I have an array of shape
(7, 24, 2, 1024)
I'd like an array of
(7, 24, 2048)
such that the elements on the last dimension are interleaving the
elements from the 3rd dimension
[0,0,0,0] -> [0,0,0]
[0,0,1,0] -> [0,0,1]
[0,0,0,1] -> [0,0,2]
[0,0,1,1] -> [0,0,3]
...
What might be the simplest way to do this?
np.transpose(A, (-2, -1)).reshape(A.shape[:-2] + (-1,))
Transpose needs a slightly different input. If you look at the help, it
should be clear. The help might also point to np.swapaxes, which may be
a bit more straight forward for this exact case.
Sorry about that. Was in a rush and working from a faulty memory.
Sebastian Berg
2015-11-17 16:23:45 UTC
Permalink
Post by Neal Becker
I have an array of shape
(7, 24, 2, 1024)
I'd like an array of
(7, 24, 2048)
such that the elements on the last dimension are interleaving the elements
from the 3rd dimension
Which basically means you want to reshape with the earlier index varying
faster. This is fortran order (in the simplest case for two axes being
reshaped). So you can do:

arr.reshape((7, 24, -1), order="F")

otherwise, if order seems too confusing or dangerous. Just transpose the
two axes first:

arr_r = arr.transpose((0, 1, 3, 2))
arr_r = arr_r.reshape((7, 24, -1))

- Sebastian
Post by Neal Becker
[0,0,0,0] -> [0,0,0]
[0,0,1,0] -> [0,0,1]
[0,0,0,1] -> [0,0,2]
[0,0,1,1] -> [0,0,3]
...
What might be the simplest way to do this?
------------
A different question, suppose I just want to stack them
[0,0,0,0] -> [0,0,0]
[0,0,0,1] -> [0,0,1]
[0,0,0,2] -> [0,0,2]
...
[0,0,1,0] -> [0,0,1024]
[0,0,1,1] -> [0,0,1025]
[0,0,1,2] -> [0,0,1026]
...
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Loading...