Numpy - Fancy Indexing



Fancy Indexing in NumPy

Fancy indexing in NumPy is a method to select multiple elements from an array using arrays or lists of specific index where, index is used to represent the position of element in the array. Instead of picking elements one by one, you can select multiple elements at once on your choice.

It's like giving the array a list of "indices" you want, and it gives you those values directly, making data handling faster and quicker.

Fancy indexing is the advance form of simple indexing. In simple indexing we access single elements or slices using integers, while fancy indexing access multiple elements using arrays or lists of integers. It returns a new array that is independent of the original one.

Fancy indexing allows you to access multiple elements using the following −

  • Another NumPy Array

  • Python List

Example: Simple Indexing

Let us create a 1D array to access a single element using it's position. In the below code arr[3] access the element at index 3 (fourth position) of array which is 60.

import numpy as np
x = np.array([50, 90, 70, 60, 40, 100])
print("By using simple indexing:" ,x[3])

Following is the output of the above code −

By using simple indexing: 60

Fancy Indexing Using a NumPy Array

Let us create a 1D array using arange() function that includes numbers from 20 to 30, then we will create a second NumPy array for indexing multiple elements at once.

In the below code we have created a second array which contains indices to access multiple elements i.e. 3, 4, 6 and the elements in those positions are 23, 24, 26.

import numpy as np
x = np.arange(20, 31)
print(x)
arr = np.array([ 3, 4, 6])
print("The elements at 3, 4, 6 positions are :\n" , x[arr])

Following is the output of the above code −

[20 21 22 23 24 25 26 27 28 29 30]
The elements at 3, 4, 6 positions are :
 [23 24 26]

Fancy Indexing Using a Python List

Let us create a 1D array using randint which generates random integers. Then by using python list we will store the positions we want to access. Here [1, 0, 2] is stored in indices, then we will use the indices list to access the array.

import numpy as np
array = np.random.randint(10, 59, size = 10)
print(array)
indices = [1, 0, 2]
print("Accessing multiple elements using python list: \n", array[indices])

Following is the output of the above code −

[32 57 48 26 47 32 38 35 30 36]
Accessing multiple elements using python list: 
 [57 32 48]

Converting 1D to 2D Array Using Fancy Indexing

In this example, we used the arange() function to build a one-dimensional array containing numbers from 1 to 10. Here the elements we need to access is specified in 2D. In fancy indexing the shape of result is reflected by the shape of index array. Following is the code −

import numpy as np
x = np.arange(1, 10)
indices = np.array([[5, 3], [4, 5]])
new_2D_arr = x[indices]
print(new_2D_arr)

Following is the output of the above code −

[[6 4]
 [5 6]]

Fancy Indexing in 2D NumPy Array

In the below example we have used fancy indexing to select multiple elements from the 2D array. The row and column indices are provided in lists and the code selects the specified elements from the 2D array.

import numpy as np
x = np.arange(12)
x_2D = x.reshape(3,4)
row_indices = [ 1, 2]
col_indices = [0, 2]
selected_indices = x_2D[row_indices, col_indices]
print("2D array is :\n", x_2D)
print("selected elements are : \n", selected_indices)

Following is the output of the above code −

2D array is :
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
selected elements are : 
 [ 4 10]

Fancy Indexing in 3D NumPy Array

In the below example we have created 3D array and specified depth_indices, row_indices, col_indices to select particular multiple elements using fancy indexing.

import numpy as np
x = np.arange(27)
x_3D = x.reshape(3, 3, 3)
depth_indices = [0, 1]
row_indices = [ 1, 2]
col_indices = [0, 2]
selected_indices = x_3D[depth_indices, row_indices, col_indices]
print("3D array is :\n", x_3D)
print("selected elements in the 3D array : \n", selected_indices)

Following is the output of the above code −

3D 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]]]
selected elements in the 3D array : 
 [ 3 17]

Fancy Indexing with Negative Indices

With the help of fancy indexing we can use negative indices to access multiple elements from the end of the array. Following is the example for fancy indexing with negative indices.

import numpy as np
x = np.arange(10)
indices = np.array([-1, -2, -3])  
print("Selected elements:", x[indices])

Following is the output of the above code −

Selected elements: [9 8 7]
Advertisements