Indexing and memory
Indexing and slicing in one and two dimensions, views versus copies, and boolean masks for filtering and updating.
About 5 hours of study
Learning objectives
- Index and slice 1-D and 2-D arrays
- Tell views from copies
- Filter and update with masks
Lessons
- 1
Indexing and slicing Intermediate
Reading elements and sub-arrays: one index per axis separated by commas, negative indices, slices with start, stop and step on each axis, and how an integer index drops a dimension while a slice keeps it.
- 2
Views and copies Intermediate
Why slicing a NumPy array does not copy it, how writes through a view change the original, which operations copy (fancy indexing, masks, copy, astype), and how to tell them apart.
- 3
Boolean masks and where Intermediate
Comparisons that give arrays of True and False, selecting with a mask, combining conditions with &, | and ~, counting matches, np.where, and assigning through a mask.
Module practice
- NumPy: Read elements by indexEasy
- NumPy: Slice a 1-D arrayMedium
- NumPy: Index a 2-D array with integers and slicesHard
- NumPy: View or copy?Easy
- NumPy: Writes through viewsMedium
- NumPy: The strides of a 2-D viewHard
- NumPy: Print a mask and filter with itEasy
- NumPy: Choose values with np.whereMedium
- NumPy: Change marks with masked assignmentsHard