numpy.roll() in Python Last Updated : 08 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report The numpy.roll() function rolls array elements along the specified axis. Basically what happens is that elements of the input array are being shifted. If an element is being rolled first to the last position, it is rolled back to the first position. Syntax : numpy.roll(array, shift, axis = None) 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 # Python Program illustrating # numpy.roll() method import numpy as geek array = geek.arange(12).reshape(3, 4) print("Original array : \n", array) # Rolling array; Shifting one place print("\nRolling with 1 shift : \n", geek.roll(array, 1)) # Rolling array; Shifting five places print("\nRolling with 5 shift : \n", geek.roll(array, 5)) # Rolling array; Shifting five places with 0th axis print("\nRolling with 2 shift with 0 axis : \n", 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 2 shift with 0 axis : [[ 4 5 6 7] [ 8 9 10 11] [ 0 1 2 3]] These codes won't run on online IDE's. So please, run them on your systems to explore the working. Comment More infoAdvertise with us Next Article numpy.roll() in Python M Mohit Gupta Improve Article Tags : Python Python-numpy Python numpy-arrayManipulation Practice Tags : python Similar Reads numpy.rot90() in Python The numpy.rot90() method performs rotation of an array by 90 degrees in the plane specified by axis(0 or 1). Syntax: numpy.rot90(array, k = 1, axes = (0, 1)) Parameters : array : [array_like]i.e. array having two or more dimensions. k : [optional , int]No. of times we wish to rotate array by 90 degr 2 min read numpy.rollaxis() function | Python numpy.rollaxis() function roll the specified axis backwards, until it lies in a given position. Syntax : numpy.rollaxis(arr, axis, start=0) Parameters : arr : [ndarray] Input array. axis : [int] The axis to roll backwards. The positions of the other axes do not change relative to one another. start 1 min read numpy.reshape() in Python In Python, numpy.reshape() function is used to give a new shape to an existing NumPy array without changing its data. It is important for manipulating array structures in Python. Let's understand with an example:Pythonimport numpy as np # Creating a 1D NumPy array arr = np.array([1, 2, 3, 4, 5, 6]) 3 min read numpy.stack() in Python NumPy is a famous Python library used for working with arrays. One of the important functions of this library is stack(). Important points:stack() is used for joining multiple NumPy arrays. Unlike, concatenate(), it joins arrays along a new axis. It returns a NumPy array.to join 2 arrays, they must 6 min read numpy.log() in Python The numpy.log() is a mathematical function that helps user to calculate Natural logarithm of x where x belongs to all the input array elements. Natural logarithm log is the inverse of the exp(), so that log(exp(x)) = x. The natural logarithm is log in base e. Syntax :numpy.log(x[, out] = ufunc 'log1 4 min read Like