numpy.rollaxis() – The NumPy rollaxis Python Function

Introduction to NumPy rollaxis Python Function

Guide to NumPy rollaxis Python Function – The numpy.rollaxis() function reverses the provided axis until it reaches a specified location.

Syntax:

numpy.rollaxis(arr, axis, start=0)

Parameters:

  • arr : [ndarray] Input array.
  • axis : [int] The axis to roll backward. The positions of the other axes do not change relative to one another.
  • start : [int, optional] The axis is rolled until it lies before this position. The default, 0, results in a “complete” roll.

Return: [ndarray] In earlier NumPy versions, arr is returned only if the order of the axes is changed, otherwise the input array is returned. For NumPy >= 1.10.0, a view of arr is always return.

Code Examples of NumPy rollaxis Python Function

Example 01:

# welcome to softhunt.net
# Python program explaining
# numpy.rollaxis() function

# importing numpy as np
import numpy as np

arr = np.ones((4, 3, 2, 1))

softhunt = np.rollaxis(arr, 3, 1).shape

print (softhunt)

Output:

(4, 1, 3, 2)

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

Example 02:

# welcome to softhunt.net
# Python program explaining
# numpy.rollaxis() function

# importing numpy as np
import numpy as np


arr = np.ones((4, 3, 2, 1))

softhunt = np.rollaxis(arr, 2).shape

print (softhunt)

Output:

(2, 4, 3, 1)

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

FAQs

How do I roll a NumPy array?

The numpy.roll() function moves elements in an array along a given axis. Essentially, the items of the input array are shifted. It is roll back to the initial position if an element is roll first to the final place.

Difference between NumPy moveaxis and rollaxis

moveaxis: The numpy.moveaxis() function repositions the axes of an array. The order of the other axes remains unchanged.

rollaxis: The numpy.rollaxis() function reverses the provided axis until it reaches a specified location.

What does NumPy roll do?

Array items are roll along a specified axis. Rolling elements are reintroduce at the initial position if they roll past the last place.

NumPy rollaxis Python Function: Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. numpy.moveaxis() – The NumPy moveaxis Python Function

Leave a Comment