Just as you might look up information in a table using row and column numbers, or find a specific house on a street using its address number, you need a way to pinpoint and retrieve individual values stored within a NumPy array. This process uses indexing.
The simplest case is a one-dimensional array, which is like a list or sequence of values. In Python, and therefore in NumPy, indexing starts at 0. This means the first element is at index 0, the second element is at index 1, and so on. This is often called zero-based indexing.
Let's create a 1D array:
import numpy as np
# Create a simple 1D array
arr1d = np.array([10, 11, 12, 13, 14])
print(arr1d)
[10 11 12 13 14]
To access an element, you use square brackets []
after the array name, placing the index inside:
# Access the first element (index 0)
first_element = arr1d[0]
print(f"First element (at index 0): {first_element}")
# Access the third element (index 2)
third_element = arr1d[2]
print(f"Third element (at index 2): {third_element}")
First element (at index 0): 10
Third element (at index 2): 12
NumPy also supports negative indexing, which is useful for accessing elements from the end of the array. Index -1
refers to the last element, -2
refers to the second-to-last, and so forth.
# Access the last element (index -1)
last_element = arr1d[-1]
print(f"Last element (at index -1): {last_element}")
# Access the second-to-last element (index -2)
second_last = arr1d[-2]
print(f"Second-to-last element (at index -2): {second_last}")
Last element (at index -1): 14
Second-to-last element (at index -2): 13
Trying to access an index outside the valid range (0 to length-1, or -1 to -length) will result in an IndexError
. It's important to ensure your index is within the bounds of the array's size.
Two-dimensional arrays, or matrices, have rows and columns. To access a single element in a 2D array, you need to specify both the row index and the column index. Again, both row and column indices are zero-based.
The standard syntax is array[row_index, column_index]
. Notice the comma separating the row and column indices within the square brackets.
Let's create a 2D array:
# Create a 2x3 array (2 rows, 3 columns)
arr2d = np.array([[1, 2, 3],
[4, 5, 6]])
print("Original 2D Array:\n", arr2d)
Original 2D Array:
[[1 2 3]
[4 5 6]]
Now, let's access some elements:
# Access element at row 0, column 0 (top-left)
element_00 = arr2d[0, 0]
print(f"Element at [0, 0]: {element_00}")
# Access element at row 1, column 2 (bottom-right)
element_12 = arr2d[1, 2]
print(f"Element at [1, 2]: {element_12}")
# Access element at row 0, column 1
element_01 = arr2d[0, 1]
print(f"Element at [0, 1]: {element_01}")
Element at [0, 0]: 1
Element at [1, 2]: 6
Element at [0, 1]: 2
Think of the first index selecting the row and the second index selecting the column within that row.
Visual representation of 2D array indexing. The row index comes first, followed by the column index. The highlighted cell
arr2d[1, 1]
corresponds to the element at row index 1 and column index 1.
The concept naturally extends to arrays with more dimensions. For an N-dimensional array, you provide N indices separated by commas within the square brackets: array[index_dim0, index_dim1, ..., index_dimN-1]
.
Consider a 3D array (which you can think of as layers, or pages, of 2D arrays):
# Create a 2x2x3 array (2 layers, 2 rows, 3 columns)
arr3d = np.array([[[ 0, 1, 2],
[ 3, 4, 5]], # First layer (index 0)
[[ 6, 7, 8],
[ 9, 10, 11]]]) # Second layer (index 1)
print("Original 3D Array shape:", arr3d.shape)
Original 3D Array shape: (2, 2, 3)
To access an element, we need three indices: layer, row, and column.
# Access element in layer 1, row 0, column 2
element_102 = arr3d[1, 0, 2]
print(f"Element at [1, 0, 2]: {element_102}")
# Access element in layer 0, row 1, column 1
element_011 = arr3d[0, 1, 1]
print(f"Element at [0, 1, 1]: {element_011}")
Element at [1, 0, 2]: 8
Element at [0, 1, 1]: 4
Accessing single elements using indices like arr[i]
or arr[i, j]
retrieves the specific value stored at that position. This is the most fundamental way to interact with individual data points in your NumPy arrays. In the following sections, we will build upon this to select multiple elements at once using slicing and other advanced indexing techniques.
© 2025 ApX Machine Learning