NumPy · Chapter 3 of 18
Indexing and slicing
By the end of this chapter you can pick any element, row, column, block or stretch of time out of an array with indexes and slices (a[i], a[start:stop:step], a[r, c], day[b, r, c], ...), say the shape of what comes back before you run it, turn a block number into a time of day, and write into part of an array. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Arrays from zero
- 4 tasks
- NumPy
- In the all labs plan
The first 2 chapters of NumPy are free. This one comes with the all labs plan.
Why you need this
The day's readings are one array of shape (96, 40, 60), and nobody at DhoopGrid wants all 230,400 numbers at once. Bhanwar wants one panel's day to see when it woke up and when it stopped. Rukmini wants block C at noon, because block C has the new panels. The plant's wiring joins every other column into one string, and Bhanwar walks each row from the east end. Each of these is a piece of the array.
With lists you would write a loop for each piece: a loop over the blocks to collect one panel, a loop over rows 20 to 29 and columns 0 to 59 for block C, an index variable going up by two for every other column. Every loop is a chance to be off by one, and every one is slow. NumPy gives each piece in one expression, as fast as reading one number.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
noon_grid.py: block C at noon
Write noon_grid(day) in noon_grid.py. day is the day's array, shape (96, 40, 60): 96 blocks of 15 minutes, 40 rows, 60 columns. Return block C's grid at noon: block 48 (12:00 to 12:15), the plant's rows 20 to 29 and every column, as floats of shape (10, 60). Row 0 of the result is the plant's row 20, and row 9 is row 29.
The Check also gives it another made-up day of the same shape, so take every value from day.
- Task 2
one_panel.py: one panel through the day
Write three functions in one_panel.py. day has shape (96, 40, 60) and the panel is at row r, column c.
panel_day(day, r, c) returns the panel's 96 readings, one for each block in order, as floats of shape (96,).
morning(day, r, c) returns the panel's readings from 00:00 up to noon: the first 48 blocks (block 48 is noon), shape (48,).
reading_at(day, r, c, hour, minute) returns one number, the panel's reading in the block that holds that time. Block b starts at b // 4 hours and (b % 4) * 15 minutes, so 14:20 is in block 57, which starts at 14:15, and 06:59 is in block 27, which starts at 06:45.
The Check tries panels (0, 0), (25, 33), (7, 41) and (39, 59), and the times 12:00, 14:20, 06:59, 00:00, 23:45 and 09:14, on the day and on another made-up day.
- Task 3
odd_strings.py: every other column, and a row from the east
Write two functions in odd_strings.py.
odd_strings(grid) takes a 2-D grid, rows by columns, and returns every row but only columns 1, 3, 5 and so on to the end (every other column, starting at column 1). For a grid of shape (40, 60) the result has shape (40, 30); for (3, 7), shape (3, 3). The Check tries the noon grid, a grid of shape (3, 7), and a grid of one column, shape (2, 1), which has no column 1, so the answer is an empty array of shape (2, 0).
from_east(row) takes a 1-D row and returns the same readings in reverse order, the last column first, with the same shape. The Check tries row 12 at noon, a row of 7 and a row of one.
- Task 4 · interview question
Interview: column.py, a column that is a row
Bhanwar's column_at(day, b, c) should return one column of the plant at one block: all 40 panels of column c at block b, row 0 first, as floats of shape (40,). It returns day[b][:][c], and column_at(day, 48, 5) has shape (60,), not (40,), while column_at(day, 40, 45) stops with IndexError: index 45 is out of bounds for axis 0 with size 40. Fix it. The Check tries blocks 0, 40, 48 and 95 with columns 0, 5, 45 and 59, on the day and on another made-up day.
Imran will ask: day[48][20:30] and day[48, 20:30] agree. Why do day[48][:][5] and day[48, :, 5] not?
What you will learn
- Indexes: a[i] for one element, counting from 0, and from the end with negative numbers.
- Slices: a[start:stop:step], which stops before stop, and ::-1 to go backwards.
- Several axes: a[r, c], a row a[r], a column a[:, c], a block a[r0:r1, c0:c1], and the day's three axes with ....
- Blocks and times: block b starts at b // 4 hours and (b % 4) × 15 minutes.
- The rank trap: an index removes an axis, a slice keeps it.
- Writing: putting a value or an array into an element or a slice.
More in Arrays from zero
Build "Indexing and slicing" yourself
DhoopGrid runs a solar plant in the Thar desert with 2,400 panels. You turn its raw readings into answers. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of NumPy are free. This one comes with the all labs plan.