P02.3 · Indexing, slicing and views
P02.3 L1 · Core 4 lessons · about 100 min Live · in review
Part of P02 Scientific Python: NumPy, pandas and Plotting in the Programming and Data track.
Most of working with data is choosing which numbers to look at: one feature, a batch of examples, the readings above a threshold, the rows in a new order. These four lessons read the running temperature array by position and by slice, by a boolean mask and by a list of positions, each built first as a loop over nested lists. They end on the question every NumPy user meets: which of these selections is a view onto the same memory, so that a write through it changes the original, and which is a separate copy.
What you need first. Lists, slices and copies of lists (P01.6), a class with special methods (P01.9), arrays, shapes and axes (P02.1), and reductions and the broadcasting rule (P02.2).
Lessons
Positions and slices P02.3.1 25 min
Read one entry, a row, a column or a block of a 2-D array with one bracket: a position or a start:stop:step slice per axis. See why an integer drops its axis while a slice keeps it, and how None adds an axis ready for broadcasting.
Boolean masks P02.3.2 25 min
A comparison on an array gives an array of True and False. Used as an index, it keeps the entries or rows where it is True. Count and average masks, compute a classifier’s accuracy as (pred == y).mean(), combine conditions with &, | and ~, and choose values with np.where.
Picking and ordering with integer arrays P02.3.3 25 min
A list of positions used as an index picks rows in any order, with repeats. Sort the days by hill temperature with np.argsort, shuffle features and targets with one seeded permutation as a train-test split does, and see why two position lists pick entries rather than a block.
Views and copies P02.3.4 25 min
A NumPy slice is a view, a second window onto the same memory, so writing through it changes the original, while a mask or a list of positions returns a copy. Build a View class in plain Python, test arrays with np.shares_memory and .base, and avoid scaling a slice of training data in place.
The interactive
P02.3-X1 · Index explorer. A 5 by 6 grid of numbered cells. Choose a slice for the rows and the columns, a threshold mask or a list of row positions, and see the chosen cells light up beside the result and its shape. Then write zeros into the result: the grid changes for a slice, a view, and stays as it was for a mask or a list, a copy. It lives in Views and copies.