w3resource logo


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.

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:

Improve this sample solution and post your code through Disqus