Python Forum
Difficulty understanding .moveaxis in Numpy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Difficulty understanding .moveaxis in Numpy
#1
Shocked 
I am having difficulties understanding .moveaxis in Numpy.

I first create an array by using a=np.arange(24).reshape(2,3,4). The system will first fill up axis 2 with 0 1 2 3, then move along axis 1 to the next row. When the first 'page' is done, the system move along axis 0. The following is obtained.

Output:
array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]])
If I input b = np.swapaxes(a,0,2); b now, the system should fill up axis 0 first, then axis 1 and finally axis 2. The following is obtained.

Output:
array([[[ 0, 12], [ 4, 16], [ 8, 20]], [[ 1, 13], [ 5, 17], [ 9, 21]], [[ 2, 14], [ 6, 18], [10, 22]], [[ 3, 15], [ 7, 19], [11, 23]]])
This is understandable as axis 1 is preserved, so we can still see columns like 0 4 8 and 1 5 9 after swapping the axes.

But I don't really understand how .moveaxis works. If I input b = np.moveaxis(a,0,2); b, the following is obtained.

Output:
array([[[ 0, 12], [ 1, 13], [ 2, 14], [ 3, 15]], [[ 4, 16], [ 5, 17], [ 6, 18], [ 7, 19]], [[ 8, 20], [ 9, 21], [10, 22], [11, 23]]])
I know that the .moveaxis function is meant to 'move axis 0 to a new position while the other axes remain the same order', but what is the meaning of that? I understand that the result should be an array with shape (3, 4, 2), but why would the system go down the first column in the first place then move on to the second page? If axis 0 is now moved to the last place, shouldn't the system fill up the array along axis 0 first?
Reply
#2
Let us call the original axes A B C instead of 0, 1, 2. Initially
  • moving along axis A changes page: 0 -> 12 (slow axis)
  • moving along axis B changes row: 0 -> 4
  • moving along axis C changes element: 0 -> 1 (fast axis)
In array b after the moveaxis operation, the axes are now in the order B C A so
  • page changes along axis B: 0 -> 4 (now the slow axis)
  • row changes along axis C: 0 -> 1
  • element changes along axis A: 0-> 12 (now the fast axis)
I hope it helps.

An experiment
>>> import numpy as np
>>> a=np.arange(24).reshape(2,3,4)
>>> b = np.moveaxis(a, 0, 2)
>>> for i in range(2):
...     for j in range(3):
...         for k in range(4):
...             assert a[i][j][k] == b[j][k][i]
... 
>>> 
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
(Aug-05-2024, 04:40 PM)Gribouillis Wrote: Let us call the original axes A B C instead of 0, 1, 2. Initially
  • moving along axis A changes page: 0 -> 12 (slow axis)
  • moving along axis B changes row: 0 -> 4
  • moving along axis C changes element: 0 -> 1 (fast axis)
In array b after the moveaxis operation, the axes are now in the order B C A so
  • page changes along axis B: 0 -> 4 (now the slow axis)
  • row changes along axis C: 0 -> 1
  • element changes along axis A: 0-> 12 (now the fast axis)
I hope it helps.

An experiment
>>> import numpy as np
>>> a=np.arange(24).reshape(2,3,4)
>>> b = np.moveaxis(a, 0, 2)
>>> for i in range(2):
...     for j in range(3):
...         for k in range(4):
...             assert a[i][j][k] == b[j][k][i]
... 
>>> 

This is very clear. Thank you for your explanation!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Having difficulty finding python.h on OS X catsonmars 2 276 Aug-11-2024, 06:18 AM
Last Post: Pedroski55
  Having difficulty with threads and input() sawtooth500 13 1,393 Jun-07-2024, 08:40 AM
Last Post: Gribouillis
  Difficulty in adapting duplicates-filter in script ledgreve 5 1,286 Jul-17-2023, 03:46 PM
Last Post: ledgreve
  Difficulty with installation standenman 2 1,380 May-03-2023, 06:39 PM
Last Post: snippsat
  Difficulty with installation standenman 0 936 May-02-2023, 08:33 PM
Last Post: standenman
  NumPy Side Effects - HELP UNDERSTANDING lasek723 3 4,028 Sep-17-2020, 06:56 AM
Last Post: lasek723
  Difficulty in understanding transpose with a tuple of axis numbers in 3-D new_to_python 0 1,716 Feb-11-2020, 06:03 AM
Last Post: new_to_python
  Difficulty installing Pycrypto KipCarter 4 14,671 Feb-10-2020, 07:54 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020