How to Perform Advanced Array Indexing in NumPy

Updated: January 23, 2024 By: Guest Contributor Post a comment

Introduction

NumPy, short for Numerical Python, is a foundational package for scientific computing in Python. It introduces an array object class called ndarray, which allows you to work efficiently with large multidimensional arrays. A powerful feature of NumPy arrays is the ability to index them in various advanced ways. In this tutorial, we’ll explore the different methods of advanced array indexing you can perform with NumPy, from basic to more sophisticated techniques.

Prerequisites

Before proceeding, please ensure you have NumPy installed. If not, you can install it using the following command:

pip install numpy

You should also have a basic understanding of NumPy arrays and how to use them. Knowledge of Python lists and indexing rules is helpful as well.

Basic Indexing

Let’s start with a quick recap of basic indexing with NumPy arrays:


import numpy as np

# Create a basic array
arr = np.array([1, 2, 3, 4, 5])

# Index the individual element
print(arr[2]) # Output: 3

Boolean Indexing

One form of advanced indexing is using boolean indexes to filter out data. Let’s suppose you want to filter out all values greater than 2:


bool_idx = arr > 2
print(arr[bool_idx]) # Output: [3, 4, 5]

You can also provide the condition directly:


print(arr[arr > 2]) # Output: [3, 4, 5]

Integer Array Indexing

NumPy also allows indexing with other arrays:



# Create an array for index
index_array = np.array([1, 3, 4])

# Index with integer array
print(arr[index_array]) # Output: [2, 4, 5]

This technique becomes especially powerful in multidimensional scenarios:


# Create a 2D array
arr_2d = np.array([[1, 2], [3, 4], [5, 6]])

# Provide row and column index
print(arr_2d[[0, 1, 2], [0, 1, 0]]) # Output: [1 4 5]

Fancy Indexing

Fancy indexing refers to indexing with integer arrays.


# Index multiple times
print(arr_2d[[0, 0], [1, 1]]) # Output: [2 2]

You can combine it with slicing:



# Column remains the same while row changes
print(arr_2d[[0, 1, 2], :1])
# Output:
# [[1]
#  [3]
#  [5]]

Index Arrays with Broadcasting

Broadcasting extends the ability of NumPy to perform operations on arrays of different shape. This principle is also applied in indexing:


rows = np.array([[0, 0], [2, 2]])
cols = np.array([[0, 1], [0, 1]])

# Index with broadcasting
print(arr_2d[rows, cols])
# Output:
# [[1 2]
#  [5 6]]

Combined Indexing

You can combine different types of indexing methods to perform more complex operations.


# Combine boolean with integer indexing
row_bool = np.array([True, False, True])
print(arr_2d[row_bool, [1, 1, 1]]) 
# Output: [2 6]


# Combine slicing with fancy indexing
print(arr_2d[1:3, [1, 0]])
# Output:
# [[4 3]
#  [6 5]]

Conclusion

In this tutorial, we’ve explored several advanced array indexing techniques provided by NumPy, each with its distinct use-cases. Combined appropriately, they offer you powerful ways to select and manipulate data within arrays. By mastering these techniques, you can take full advantage of the efficiency and capabilities of NumPy arrays in your data analysis or scientific computing tasks.