w3resource
Python Exercises

Python NumPy: Move axes of an array to new positions

Python NumPy: Array Object Exercise-53 with Solution

Write a Python program to move the specified axis backwards, until it lies in a given position.
Move the following 3rd array axes to first position.
(2,3,4,5)

Pictorial Presentation:

Python NumPy: Move axes of an array to new positions

Sample Solution:-

Python Code:

import numpy as np
x = np.ones((2,3,4,5))
print(np.rollaxis(x, 3, 1).shape)

Sample Output:

(2, 5, 3, 4)

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to move axes of an array to new positions. Other axes remain in their original order.
Next: Write a Python program to convert specified inputs to arrays with at least one dimension.

What is the difficulty level of this exercise?