Indexing, Slicing, and Fancy Indexing
Access and modify array elements with advanced indexing techniques.
Basic Indexing
Access elements with zero-based integer indices. Negative indices count from the end. Multi-dimensional arrays use comma-separated indices.
import numpy as np
arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(arr[0, 2]) # 3
print(arr[-1, -1]) # 9
print(arr[1]) # [4 5 6]Slicing
Slicing syntax start:stop:step returns a view. Applies independently on each dimension.
import numpy as np
m = np.arange(16).reshape(4,4)
print(m[1:3, 1:3])
# [[5 6]
# [9 10]]
print(m[::2, ::2]) # every other row and columnAll lessons in this course
- NumPy Arrays and dtypes
- Array Operations and Broadcasting
- Indexing, Slicing, and Fancy Indexing
- Linear Algebra with NumPy