Is there a clean way to create a view on an existing ND-array with its axes in a different order. For example, suppose I have an array of shape (100,200,300,3) and I want to create a view of this where the vector coordinate is axis 0, not axis 3. (So the view will have shape (3,100,200,300).) Reading the help(numpy.ndarray) output I can't find anything better than repeated calls to swapaxes():
B = A.swapaxes(0,3).swapaxes(1,3).swapaxes(2,3)
Is there a "reorder_axes()" method that would let me write something like this:
B = A.reorder_axes((3,0,1,2))
Apologies in advance if I've missed the obvious method in the docs.
On Fri, May 30, 2014 at 4:48 PM, Bob Dowling <rjd4+numpy@cam.ac.uk> wrote:
Is there a clean way to create a view on an existing ND-array with its axes in a different order.
For example, suppose I have an array of shape (100,200,300,3) and I want to create a view of this where the vector coordinate is axis 0, not axis 3. (So the view will have shape (3,100,200,300).)
Reading the help(numpy.ndarray) output I can't find anything better than repeated calls to swapaxes():
B = A.swapaxes(0,3).swapaxes(1,3).swapaxes(2,3)
Is there a "reorder_axes()" method that would let me write something like this:
B = A.reorder_axes((3,0,1,2))
Apologies in advance if I've missed the obvious method in the docs.
http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.transpose.... -- Robert Kern
http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.transpose....
And I completely missed its general case. D'oh! Thank you.
On Fri, May 30, 2014 at 4:55 PM, Bob Dowling <rjd4+numpy@cam.ac.uk> wrote:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.transpose....
And I completely missed its general case. D'oh!
Don't feel bad; it's not often discussed, and has a name derived from its rank-2 special case. :-) -- Robert Kern
On Fri, May 30, 2014 at 8:48 AM, Bob Dowling <rjd4+numpy@cam.ac.uk> wrote:
Is there a clean way to create a view on an existing ND-array with its axes in a different order.
There's an epidemic of axes reordering, the exact same thing was asked yesterday in StackOverflow: http://stackoverflow.com/questions/23943379/swapping-the-dimensions-of-a-num... Aside from the general solution provided by Robert, for your use case, where you just want to move a single axis to a different position, you may want to use `np.rollaxis`: http://docs.scipy.org/doc/numpy/reference/generated/numpy.rollaxis.html Jaime -- (\__/) ( O.o) ( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial.
participants (3)
-
Bob Dowling
-
Jaime Fernández del Río
-
Robert Kern