NumPy rollaxis() Function:
The NumPy rollaxis() function rolls the provided axis backwards until it lies in a specified position.
Syntax:
numpy.rollaxis(a, axis, start=0)
Parameters
a: This is required. It is the input array(ndarray).
axis: This is required. It indicates the axis that must be rolled. The other axes positions do not change in relation to one another.
start: This is optional. The axis is rolled back until it is in this position when start = axis. When start > axis, the axis is rolled until it lies before this position. The default value is 0 produces a ‘full/complete’ roll.
Return value:
A view of array(a) is returned.
- Python NumPy hstack() Function
- Python: Count Number of True Elements in Numpy Array in Python
- Count occurrences of a value in NumPy array in Python | numpy.count() in Python
NumPy rollaxis() Function in Python
Example1
Here, the rollaxis() function is used to roll the axis of an array backwards.
Approach:
- Import numpy module using the import keyword.
- Pass some random values for the ones() function. It creates an array of the given shape values.
- Print the shape of the given array.
- Pass the given array, axis values, start value to the rollaxis() function (It rolls the array backwards), and print the shape of it.
- Similarly check for the different axis and print the shape of it.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Pass some random values for the ones() function. It creates an array # of the given shape values gvn_arr = np.ones((1,2,3,4)) # Print the shape of the given array print(gvn_arr.shape) # Pass the given array, axis values, start value to the rollaxis() # function(It rolls the array backwards) and print the shape of it. print(np.rollaxis(gvn_arr, 2, 1).shape) # Similary check for the different axis (here we didn't give start value) # and print the shape of it. print(np.rollaxis(gvn_arr, 3).shape) print(np.rollaxis(gvn_arr, 1, 4).shape)
Output:
(1, 2, 3, 4) (1, 3, 2, 4) (4, 1, 2, 3) (1, 3, 4, 2)
Example2
Approach:
- Import numpy module using the import keyword.
- Create an array of length some random number n using the arange() function (which creates an array of values 0 to n-1) and apply the reshape() function by passing the shape values to it.
- Store it in a variable.
- Pass the given array, axis values, start value to the rollaxis() function (It rolls the array backwards), and print it.
- Print the given array.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Create an array of length some random number n using the arange() function #(which creates an array of values 0 to n-1) and apply reshape() function # by passing the shape values to it. gvn_arry = np.arange(27).reshape(3,3,3) # Print the given array print("The given array is:") print(gvn_arry) # Pass the given array, axis values, start value to the rollaxis() # function (It rolls the array backwards) and print it. print("The above given array after rolling backwards:") print(np.rollaxis(gvn_arry, 1, 3))
Output:
The given array is: [[[ 0 1 2] [ 3 4 5] [ 6 7 8]] [[ 9 10 11] [12 13 14] [15 16 17]] [[18 19 20] [21 22 23] [24 25 26]]] The above given array after rolling backwards: [[[ 0 3 6] [ 1 4 7] [ 2 5 8]] [[ 9 12 15] [10 13 16] [11 14 17]] [[18 21 24] [19 22 25] [20 23 26]]]