numpy.roll () in Python



Parameters :

  array:  [array_like] [array_like] Input array, whose elements we want to roll  shift:  [int or int_tuple] No. of times we need to shift array elements. If a  tuple , then axis must be a tuple of the same size, and each of the given axes is shifted by the corresponding number. If an  int  while axis is a tuple of ints, then the same value is used for all given axes.  axis:  [array_like] Plane, along which we wish to roll array or shift it`s elements. 

Return:

 Output rolled array, with the same shape as a. 

# Python program illustrating
# numpy.roll () method

 

import numpy as geek

 

array = geek.arange ( 12 ). reshape ( 3 , 4 )

print ( "Original array: " , array)

  
# Rolling array; Move one place

print ( "Rolling with 1 shift: " , geek.roll (array, 1 ) )

 
# Rolling array; Move five places

print ( "Rolling with 5 shift: " , geek.roll (array, 5 ) )

 
# Rolling array; Shift five places with 0-th axis

print ( " Rolling with 5 shift with 0 axis: " , geek.roll (array, 2 , axis = 0 ))

Output:

 Original array: [[0 1 2 3] [4 5 6 7] [8 9 10 11]] Rolling with 1 shift: [[11 0 1 2] [3 4 5 6] [7 8 9 10] ] Rolling with 5 shift: [[7 8 9 10] [11 0 1 2] [3 4 5 6]] Rolling with 5 shift with 0 axis: [[4 5 6 7] [8 9 10 11] [0 1 2 3]] 

Links:
https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.roll.html

Notes:
These codes will not work for online ID. Please run them on your systems to see how they work. 
This article is courtesy of Mohit Gupta_OMG