Useful Indexing Techniques In NumPy

When playing with data, many times we only want to access specific elements or subsets of data. Indexing in NumPy refers to the process of accessing specific elements or subsets of elements within a NumPy array. NumPy offers various indexing techniques to work with arrays efficiently.

magnifying glass on book
Photo by Nothing Ahead on Pexels.com

Table of Contents

Basic Indexing

NumPy arrays can be indexed using square brackets and integer indices, similar to Python lists. The indexing starts from 0.

import numpy as np

arr = np.array([0, 1, 2, 3, 4])

# Accessing a single element
element = arr[2]  # Returns 2

# Slicing to get a subset of elements
subset = arr[1:4]  # Returns [1, 2, 3]
Multidimensional Indexing

NumPy supports indexing in multiple dimensions for multidimensional arrays (e.g., matrices). You can use comma-separated indices or separate brackets for each dimension.

import numpy as np

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

# Accessing a single element
element = matrix[1, 2]  # Returns 6

# Slicing rows and columns
row = matrix[0]  # Returns [1, 2, 3]
col = matrix[:, 1]  # Returns [2, 5, 8]
Boolean Indexing

You can use boolean arrays to filter elements based on a condition. This is particularly useful for selecting elements that satisfy specific criteria.

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

# Boolean indexing to select elements greater than 3
filtered = arr[arr > 3]  # Returns [4, 5]
Fancy Indexing

NumPy allows you to use arrays of integers or other sequences as indices to access or modify multiple elements simultaneously.

import numpy as np

arr = np.array([10, 20, 30, 40, 50])

# Using an array of indices
indices = np.array([0, 2, 4])
selected = arr[indices]  # Returns [10, 30, 50]
Integer Array Indexing

NumPy also provides advanced indexing techniques like integer array indexing, which allows you to create arbitrary arrays using the data from another array.

import numpy as np

arr = np.array([[1, 2], [3, 4], [5, 6]])

# Integer array indexing to select specific elements
selected = arr[[0, 1], [1, 0]]  # Returns [2, 3]
Slicing with Steps

You can use slicing with step values to access elements at regular intervals.

import numpy as np

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

# Slicing with a step of 2
subset = arr[1:8:2]  # Returns [1, 3, 5, 7]

These indexing techniques provide great flexibility for accessing and manipulating data within NumPy arrays. Depending on your data analysis or computation needs, you can choose the most appropriate method to extract the information you require from the array.