Numpy roll Explained With Examples in Python

Suppose you have an array having n number of elements in it. Then suddenly, a situation arrives where you are forced to shift each element by m position. In such cases, the function NumPy roll() comes handy. Also, it doesn’t change the shape of the parent array. This will become a lot more clear to you as we move ahead in this article.

In the next section, we will look at the syntax associated with this.

Syntax

numpy.roll(a, shift)

This is the general syntax for our function. In the next section, we will see different parameters associated with our function.

Parameters

1. a:array_like

This parameter represents the input array on which the operation needs to be performed.

2. shift:int or tuples of int

This represents the number of places by which the elements of the array need to be shifted. If it is of the type tuple, then the axis must be of the same type. If an int while the axis is a tuple of int, then the same number of values are used for all given axes.

3. Axis: Int or tuples of int

Axis along which the elements are shifted. By default, the parent array is first flattened, following which the operation is performed, and then the original shape is restored.

Examples

As we are done with all the theory portions related to NumPy roll(), This section will look at how this function works and how it helps us achieve our desired output. We will start with an elementary-level example and gradually move our way to more complicated examples.

1. Basic example for Numpy roll()

#input
import numpy as ppool
a=[[1,2,3],
   [6,4,5]]
print(ppool.roll(a,2))

Output:

[[4 5 1]
 [2 3 6]]

In the above example, we have first imported the NumPy module. After this, we defined our array. Following this, we have used the print statement along with our syntax. Let us now dissect our output, compare it with our input, and try to understand it. Here, for observation purposes, we will consider a flattened array, as discussed earlier.

Our input is [10,21,32,63,44,55]. Here, in the subscript, we have denoted its location in the array. According to our command, we want to shift the elements by 2 places each. So, according to that, our output should be [12,23,34,65,46,57]. Here, elements 4 and 5 occupy the spots that are not part of the parent array and look. We have spots 0 and 1 empty. So, these values are shifted over there, and the overall shape is retained.

Now let us look at another example and also use the axis in it.

2. Example with optional parameters

import numpy as ppool
a=[[1,2,3],
   [6,4,5],
   [9,7,8]]
print(ppool.roll(a,4,axis=1))

Output:

[[3 1 2]
 [5 6 4]
 [8 9 7]]

Another example of our function is NumPy roll(). As similar to the first example, we have first imported the NumPy module, following which we have defined our array. Then, we used our print statement with our syntax. We have also used the optional parameter axis, and what it does is rather consider the whole array it operates on its sub-arrays. Let us see what would have happened if the axis was not used.

import numpy as ppool
a=[[1,2,3],
   [6,4,5],
   [9,7,8]]
print(ppool.roll(a,4))

Output:

[[5 9 7]
 [8 1 2]
 [3 6 4]]

The difference is quite evident. So you can play with the syntax and use it according to your needs.

Must Read

CONCLUSION

In this article, we covered the NumPy roll(). Besides that, we have also looked at its syntax and parameters. For better understanding, we looked at a couple of examples. We varied the syntax and looked at the output for each case. In the end, we can conclude that NumPy roll() is a function that helps us shift the elements within the array according to our needs.

I hope this article was able to clear all of your doubts. But if you have any unsolved queries, feel free to write them down below in the comment section.

Done reading this; why not read about mgrid next?

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments