Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Numpy rollaxis() Function



The Numpy rollaxis() Function is used to roll the specified axis backwards until it lies in a given position by moving all other axes accordingly.

This function is useful for changing the order of axes in multi-dimensional arrays without modifying the array's data.

This function takes three parameters namely, the array, the axis to roll and the start position. The axis is rolled back until it reaches the start position.

Syntax

The syntax for the Numpy rollaxis() function is as follows −

numpy.rollaxis(a, axis, start=0)

Parameters

Following are the parameters of the Numpy rollaxis() Function −

  • a : The input array.
  • axis : The axis to be rolled. The axis is rolled backward until it lies in a given position.
  • start :The axis is rolled backward until it lies before this position. The default is 0 i.e., the rolled axis is moved to the front.

Return Value

This function returns the array with the axis rolled to the specified position.

Example 1

Following is the example of Numpy rollaxis() Function in which the last axis i.e. axis 2 is moved to the front by changing the shape from (2, 3, 4) to (4, 2, 3) −

import numpy as np

# Create a 3-dimensional array
a = np.arange(24).reshape(2, 3, 4)
print("Original array:")
print(a)

# Roll the last axis to the front
result = np.rollaxis(a, 2, 0)
print("Array after rolling last axis to the front:")
print(result)

Output

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

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]

Array after rolling last axis to the front:
 [[[ 0  4  8]
  [12 16 20]]

 [[ 1  5  9]
  [13 17 21]]

 [[ 2  6 10]
  [14 18 22]]

 [[ 3  7 11]
  [15 19 23]]]

Example 2

Here in this example the second axis i.e., axis 1 is moved to the last position by changing the shape from (2, 3, 4) to (2, 4, 3) −

import numpy as np

# Create a 3-dimensional array
a = np.arange(24).reshape(2, 3, 4)
print("Original array:")
print(a)

# Roll the second axis to the last position
result = np.rollaxis(a, 1, 3)
print("Array after rolling second axis to the last position:")
print(result)

Output

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

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]

Array after rolling second axis to the last position:
 [[[ 0  1  2]
  [ 4  5  6]
  [ 8  9 10]]

 [[12 13 14]
  [16 17 18]
  [20 21 22]]

 [[ 3  7 11]
  [15 19 23]]]

Example 3

Here in this example we are using the numpy.rollaxis() to manipulate the axes of a 3-dimensional NumPy array −

import numpy as np

# Creating a 3-dimensional ndarray
a = np.arange(8).reshape(2, 2, 2)

print('The original array:')
print(a)
print('\n')

# Roll axis 2 to axis 0 (along width to along depth)
print('After applying rollaxis function (axis 2 to 0):')
print(np.rollaxis(a, 2))
print('\n')

# Roll axis 2 to axis 1 (along width to height)
print('After applying rollaxis function (axis 2 to 1):')
print(np.rollaxis(a, 2, 1))

Output

The original array:
[[[0 1]
  [2 3]]

 [[4 5]
  [6 7]]]


After applying rollaxis function (axis 2 to 0):
[[[0 2]
  [4 6]]

 [[1 3]
  [5 7]]]


After applying rollaxis function (axis 2 to 1):
[[[0 2]
  [1 3]]

 [[4 6]
  [5 7]]]
numpy_array_manipulation.htm
Advertisements