[Numpy-discussion] Reordering indices

Bob Dowling rjd4+numpy at cam.ac.uk
Fri May 30 11:48:45 EDT 2014

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.

More information about the NumPy-Discussion mailing list