numpy.roll() in python and it’s working

Numpy.roll()

 

In python the numpy.roll() function is provided by the NumPy Library.
The numpy.roll() Function can Shift element of array to a specific axis.
The main application of the numpy.roll() can manipulate cyclic or periodic data.

syntax :-
numpy.roll(array, shift, axis=None)
Parameters :
  1.  Array :-  Array to be rolled.
  2. Shift :- The number of places by which elements are shifted.
  3. Axis :- The axis is Axes which elements are shifted , default is none Means array flattened before the shift.
Examples:
import numpy as np
array = np.array([10, 20, 30, 40, 50])
Rolled_array = np.roll(array, 2)
print(Rolled_array)
Output :-

[40 50 10 20 30]

Breakdown Of Code :-
import numpy as np

Explanation : 

  • This line imports the NumPy library and assigns it the np 
  • NumPy is a Python library used for support for arrays & mathematical operations
array = np.array([10, 20, 30, 40, 50])

Explanation :

  • np.array()  creates a NumPy array from the given list [10,20,30,40,50].
Rolled_array = np.roll(array, 2)

Explanation :

  • np.roll() can roll the elements of the array.
  • 2 is the shift value, meaning that the elements are shifted to the right by 2 positions.
  • After applying the roll the Value of the Rolled_array is [40,50,10,20,30]
print(Rolled_array)

Explanation :

  • This will print the rolled array
[40, 50, 10, 20, 30]

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top