How does NumPy's transpose() method permute the axes of an array?
To transpose an array, NumPy just swaps the shape and stride information for each axis. Here are the strides:
>>> arr.strides
(64, 32, 8)
>>> arr.transpose(1, 0, 2).strides
(32, 64, 8)
Notice that the transpose operation swapped the strides for axis 0 and axis 1. The lengths of these axes were also swapped (both lengths are 2
in this example).
No data needs to be copied for this to happen; NumPy can simply change how it looks at the underlying memory to construct the new array.
Visualising strides
The stride value represents the number of bytes that must be travelled in memory in order to reach the next value of an axis of an array.
Now, our 3D array arr
looks this (with labelled axes):
This array is stored in a contiguous block of memory; essentially it is one-dimensional. To interpret it as a 3D object, NumPy must jump over a certain constant number of bytes in order to move along one of the three axes:
Since each integer takes up 8 bytes of memory (we're using the int64 dtype), the stride value for each dimension is 8 times the number of values that we need to jump. For instance, to move along axis 1, four values (32 bytes) are jumped, and to move along axis 0, eight values (64 bytes) need to be jumped.
When we write arr.transpose(1, 0, 2)
we are swapping axes 0 and 1. The transposed array looks like this:
All that NumPy needs to do is to swap the stride information for axis 0 and axis 1 (axis 2 is unchanged). Now we must jump further to move along axis 1 than axis 0:
This basic concept works for any permutation of an array's axes. The actual code that handles the transpose is written in C and can be found here.
Transpose 3D Numpy Array
You need to swap the second and third axises, for which you can use either np.swapaxes
:
A.swapaxes(1,2)
#array([[[ 1, 4],
# [ 2, 5],
# [ 3, 6]],
# [[ 7, 10],
# [ 8, 11],
# [ 9, 12]]])
or transpose
:
A.transpose(0,2,1)
#array([[[ 1, 4],
# [ 2, 5],
# [ 3, 6]],
# [[ 7, 10],
# [ 8, 11],
# [ 9, 12]]])
How does NumPy's transpose() method permute the axes of an array?
To transpose an array, NumPy just swaps the shape and stride information for each axis. Here are the strides:
>>> arr.strides
(64, 32, 8)
>>> arr.transpose(1, 0, 2).strides
(32, 64, 8)
Notice that the transpose operation swapped the strides for axis 0 and axis 1. The lengths of these axes were also swapped (both lengths are 2
in this example).
No data needs to be copied for this to happen; NumPy can simply change how it looks at the underlying memory to construct the new array.
Visualising strides
The stride value represents the number of bytes that must be travelled in memory in order to reach the next value of an axis of an array.
Now, our 3D array arr
looks this (with labelled axes):
This array is stored in a contiguous block of memory; essentially it is one-dimensional. To interpret it as a 3D object, NumPy must jump over a certain constant number of bytes in order to move along one of the three axes:
Since each integer takes up 8 bytes of memory (we're using the int64 dtype), the stride value for each dimension is 8 times the number of values that we need to jump. For instance, to move along axis 1, four values (32 bytes) are jumped, and to move along axis 0, eight values (64 bytes) need to be jumped.
When we write arr.transpose(1, 0, 2)
we are swapping axes 0 and 1. The transposed array looks like this:
All that NumPy needs to do is to swap the stride information for axis 0 and axis 1 (axis 2 is unchanged). Now we must jump further to move along axis 1 than axis 0:
This basic concept works for any permutation of an array's axes. The actual code that handles the transpose is written in C and can be found here.
How does numpy.transpose work for this example?
As given in the documentation -
numpy.transpose(a, axes=None)
axes : list of ints, optional
By default, reverse the dimensions, otherwise permute the axes according to the values given.
The second argument is the axes using which the values are permuted. That is for example if the index of initial element is (x,y,z)
(where x
is 0th axes, y
is 1st axes, and z
is 2nd axes) , the position of that element in the resulting array becomes (z,y,x) (that is 2nd axes first, then 1st axes, and last 0th axes) , based on the argument you provided for axes
.
Since you are transposing an array of shape (2,2,2)
, the transposed shape is also (2,2,2)
, and the positions would change as -
(0,0,0) -> (0,0,0)
(1,0,0) -> (0,0,1)
(0,1,0) -> (0,1,0)
(1,1,0) -> (0,1,1)
...
Since the axes you choose are trivial, lets explain this for another axes. Example -
In [54]: A = np.arange(30).reshape((2, 3, 5))
In [55]: A
Out[55]:
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, 24],
[25, 26, 27, 28, 29]]])
In [56]: np.transpose(A,(1,2,0))
Out[56]:
array([[[ 0, 15],
[ 1, 16],
[ 2, 17],
[ 3, 18],
[ 4, 19]],
[[ 5, 20],
[ 6, 21],
[ 7, 22],
[ 8, 23],
[ 9, 24]],
[[10, 25],
[11, 26],
[12, 27],
[13, 28],
[14, 29]]])
Here, the first element (0,0,0)
becomes the (0,0,0)
element in the result.
The second element (0,0,1)
becomes the (0,1,0)
element in the result. And so on -
(0,0,0) -> (0,0,0)
(0,0,1) -> (0,1,0)
(0,0,2) -> (0,2,0)
...
(2,3,4) -> (3,4,2)
...
Related Topics
List Directory Tree Structure in Python
How to Set Up a Virtual Environment for Python in Visual Studio Code
How to Disable a Pylint Warning
Django Rest Framework File Upload
How to Create PDF Files in Python
How to Display Full Output in Jupyter, Not Only Last Result
Heatmap in Matplotlib with Pcolor
How to Properly Assert That an Exception Gets Raised in Pytest
Difference Between Numpy Dot() and Python 3.5+ Matrix Multiplication @
Databaseerror: Current Transaction Is Aborted, Commands Ignored Until End of Transaction Block
Share Large, Read-Only Numpy Array Between Multiprocessing Processes
Add X and Y Labels to a Pandas Plot
Python Assigning Multiple Variables to Same Value? List Behavior
Split Text Lines in Scanned Document