NumPy Fancy Indexing

In NumPy, fancy indexing allows us to use an array of indices to access multiple array elements at once.

Fancy indexing can perform more advanced and efficient array operations, including conditional filtering, sorting, and so on.


Select Multiple Elements Using NumPy Fancy Indexing

import numpy as np

# create a numpy array
array1 = np.array([1, 2, 3, 4, 5, 6, 7, 8])

# select elements at index 1, 2, 5, 7
select_elements = array1[[1, 2, 5, 7]]

print(select_elements)

# Output: [2 3 6 8]

In this example, the resulting array select_elements contains the elements of array1 that correspond to the indices [1, 2, 5, 7] which are 2, 3, 6, and 8 respectively.


Example: NumPy Fancy Indexing

import numpy as np

array1 = np.array([1, 2, 3, 4, 5, 6, 7, 8])

# select a single element
simple_indexing = array1[3]

print("Simple Indexing:",simple_indexing)   # 4

# select multiple elements
fancy_indexing = array1[[1, 2, 5, 7]]

print("Fancy Indexing:",fancy_indexing)   # [2 3 6 8]

Output

Simple Indexing: 4
Fancy Indexing: [2 3 6 8]

Note: To learn more about simple NumPy array indexing, visit NumPy Array Indexing.


Fancy Indexing for Sorting NumPy Array

Fancy indexing can also sort a NumPy array. Let's see an example.

import numpy as np

array1 = np.array([3, 2, 6, 1, 8, 5, 7, 4])

# sort array1 using fancy indexing
sorted_array = array1[np.argsort(array1)]

print(sorted_array)

# Output: [1, 2, 3, 4, 5, 6, 7, 8]

Here, we are using the fancy indexing with the argsort() function to sort the array1 in the ascending order.

We could also use fancy indexing to sort the array in descending order.

import numpy as np

array1 = np.array([3, 2, 6, 1, 8, 5, 7, 4])

# sort array1 using fancy indexing in descending order
sorted_array = array1[np.argsort(-array1)]

print(sorted_array)

# Output: [8 7 6 5 4 3 2 1]

Here, first we multiplied array1 by -1 to sort in descending order and then used the fancy indexing to return the sorted array.


Fancy Indexing to Assign New Values to Specific Elements

We can also assign new values to specific elements of a NumPy array using fancy indexing. For example,

import numpy as np

array1 = np.array([3, 2, 6, 1, 8, 5, 7, 4])

# create a list of indices to assign new values
indices = [1, 3, 6]

# create a new array of values to assign
new_values = [10, 20, 30]

# use fancy indexing to assign new values to specific elements
array1[indices] = new_values

print(array1)

# Output: [ 3 10  6 20  8  5 30  4]

In this example, first we have created a list of indices called indices which specifies the elements of array1 that we want to assign new values to.

Then we created the array for new values called new_values that we want to assign to the specified indices.

Finally, we used fancy indexing with the list of indices to assign the new values to the specified elements of array1.


Fancy Indexing on N-d Arrays

We can also use fancy indexing on multi-dimensional arrays.

Let's see an example to select specific rows using fancy indexing.

import numpy as np

# create a 2D array
array1 = np.array([[1, 3, 5], 
                [11, 7, 9], 
                [13, 18, 29]])

# create an array of row indices
row_indices = np.array([0, 2])

# use fancy indexing to select specific rows
selected_rows = array1[row_indices, :]

print(selected_rows)

Output

[[ 1  3  5]
 [13 18 29]]

Here, we have created a 2D array named array1 and an array of row indices named row_indices.

Then, we used fancy indexing to select the rows with indices 0 and 2 from array1.

Note: To learn more about N-d array indexing, visit NumPy Array Indexing.