Array indexing in NumPy is used to access, select, and modify elements of an array. NumPy supports indexing for one-dimensional and multidimensional arrays, along with slicing, Boolean indexing, fancy indexing, and tools such as np.newaxis and ellipsis (...).
NumPy Array Indexing Techniques
1. Indexing 1D Arrays
A 1D NumPy array uses zero-based indexing, where the first element is at index 0. Negative indices access elements from the end.
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr[0])
Output
10
Explanation:
- arr[0] returns the first element, 10.
- arr[-1] returns the last element, 50.
- Positive indices start from 0.
- Negative indices start from -1 at the last element.
2. Multidimensional Array Indexing
Multidimensional arrays use an index for each axis. A 2D array uses row and column indices, while a 3D array uses depth, row, and column indices.
1. 2D Array: A 2D array uses row and column indices to access a specific element.
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matrix[1, 2])
Output
6
Explanation:
- 1 selects the second row.
- 2 selects the third column.
- matrix[1, 2] returns 6.
2. 3D Array: A 3D array uses three indices in the order depth, row, column.
import numpy as np
cube = np.array([[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]],
[[10, 11, 12],
[13, 14, 15],
[16, 17, 18]]])
print(cube[1, 2, 0])
Output:
16
Explanation:
- 1 selects the second 2D block.
- 2 selects the third row.
- 0 selects the first column.
- cube[1, 2, 0] returns 16.
3. Array Slicing
Array slicing selects a range of elements using the start:stop:step.
1. 1D Arrays: 1D arrays slicing selects elements from a specified index range.
import numpy as np
arr = np.array([0, 1, 2, 3, 4, 5])
print(arr[1:4])
Output
[1 2 3]
Explanation:
- 1:4 starts slicing from index 1.
- The stop index 4 is not included.
- The result is [1 2 3].
2. Multidimensional Arrays: Slicing can be applied to each dimension to select rows, columns, or smaller sections of an array.
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matrix[0:2, 1:3])
Output
[[2 3] [5 6]]
Explanation:
- 0:2 selects the first two rows.
- 1:3 selects the second and third columns.
- matrix[0:2, 1:3] returns the selected 2 × 2 subarray.
4. Boolean Indexing
Boolean indexing selects elements that satisfy a specified condition.
import numpy as np
arr = np.array([10, 15, 20, 25, 30])
print(arr[arr > 20])
Output
[25 30]
Explanation:
- arr > 20 checks each element against 20.
- Elements greater than 20 produce True.
- Only the elements with True values are selected.
- The result is [25 30].
Multiple conditions can be combined using &, |, and ~.
Example: The combined condition selects elements greater than 10 and less than 30, resulting in [15, 20, 25].
import numpy as np
arr = np.array([10, 15, 20, 25, 30])
print(arr[(arr > 10) & (arr < 30)])
Output:
[15 20 25]
The combined condition selects elements greater than 10 and less than 30 resulting in [15, 20, 25].
5. Fancy Indexing
Fancy indexing, also called advanced indexing, allows us to select multiple elements using a list or array of integer indices. It is useful for selecting elements from specific, non-adjacent positions.
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
indices = [0, 2, 4]
print(arr[indices])
Output
[10 30 50]
Explanation:
- indices contains the positions [0, 2, 4].
- 0 selects 10.
- 2 selects 30.
- 4 selects 50.
- The result is [10 30 50].
6. Integer Array Indexing
Integer array indexing selects elements using an array of integer indices. It is useful for selecting specific elements from an array based on their positions.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
indices = np.array([1, 3])
print(arr[indices])
Output
[2 4]
Explanation:
- indices contains the integer positions [1, 3].
- Index 1 selects 2.
- Index 3 selects 4.
- The result is [2 4].
7. Ellipsis (...) in Indexing
The ellipsis ... represents unspecified dimensions in a multidimensional array. It is useful when we want to index a specific dimension without writing every preceding dimension.
import numpy as np
cube = np.arange(24).reshape(2, 3, 4)
print(cube[..., 0])
Output
[[ 0 4 8] [12 16 20]]
Explanation:
- ... represents the preceding dimensions.
- 0 selects the first element along the last dimension.
- cube[..., 0] selects index 0 from the last dimension.
- It is equivalent to cube[:, :, 0].
8. Using np.newaxis to Add a Dimension
np.newaxis adds a new axis with size 1 to an array. It can be used to convert a 1D array into a row or column vector.
import numpy as np
arr = np.array([1, 2, 3])
print(arr[:, np.newaxis])
Output
[[1] [2] [3]]
Explanation:
- : selects all elements.
- np.newaxis adds a new dimension.
- The original shape is (3,).
- The new shape is (3, 1).
9. Modifying Array Elements
Indexing and slicing can be used to modify individual elements or a range of elements.
import numpy as np
arr = np.array([1, 2, 3, 4])
arr[1:3] = 99
print(arr)
Output:
[ 1 99 99 4]
Explanation:
- 1:3 selects the elements at indices 1 and 2.
- = 99 assigns 99 to both selected elements.
- The modified array is [1 99 99 4].