NumPy · Chapter 8 of 18
Aggregation and running totals
By the end of this chapter you can add up, average and find the largest along any axis of an array with axis=, say from the shape which axis disappeared, keep it with keepdims=True, find where the largest or smallest value is with argmax and argmin (and turn that into a row and a column with np.unravel_index), and follow a total through the day with cumsum, cumprod and np.diff. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Shapes and maths
- 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
Rukmini's evening report has three lines she reads first: the plant's output in every block of the day, each block of panels' energy, and the best and the worst panel. Meenal wants the energy so far, block by block, because the exchange settles against a running schedule. All of it comes from the same (96, 40, 60) array, added up in different directions.
Chapter 5's .sum() and .max() give one number for the whole array: enough for the day's 7,708.5 kWh, and nothing about which block or which panel. To get one number per panel you would loop over the 2,400 panels and take each one's 96 readings; to get one per block, loop over the 96 blocks. Measured below, the panel loop is about 30 times slower than saying, once, which axis to add up along, and the code that does it is one word.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
block_power.py: each plant block's output
The plant's four blocks, A to D, are ten rows each: rows 0 to 9 are A, 10 to 19 B, 20 to 29 C, 30 to 39 D. In block_power.py, day has shape (n, 40, 60), in watts, one grid for each 15-minute time block. Write two functions:
block_kw(day) returns each plant block's total output in kW (1 kW is 1000 W) in every time block, as a float array of shape (n, 4), where [t, k] is plant block k at time block t: (96, 4) for the day.
block_kwh(day) returns each plant block's energy over all n time blocks in kWh, as a float array of shape (4,): a time block's energy in kWh is its kW times 0.25 h.
The Check passes the day and a made-up day of 3 time blocks.
- Task 2
best_panel.py: the best panel of the day
In best_panel.py, day has shape (n, 40, 60), in watts. Write two functions:
panel_kwh(day) returns every panel's energy over the n time blocks in kWh, as a float array of shape (40, 60): each reading's energy is its watts times 0.25 h, divided by 1000.
best_panel(day) returns the row and column of the panel with the most energy, as a tuple of two whole numbers, (row, col): on a made-up day where only panel (39, 0) makes power it is (39, 0). argmax of a 2-D array gives one flat position, and one number fails the task.
The Check passes the day, a made-up day of 3 time blocks, and made-up days where only panel (39, 0) or (2, 57) makes power.
- Task 3
half_day.py: when the day is half done
In half_day.py, day has shape (n, 40, 60), in watts. Write two functions:
energy_so_far(day) returns the plant's energy from the start of the day to the end of each time block, in kWh, as a float array of shape (n,): a time block's energy is all its panels' watts times 0.25 h, divided by 1000, and the last value is the whole day's energy, about 7,708.5 kWh for the day.
half_block(day) returns the first time block whose energy so far is at least half of the day's total, as a whole number from 0 to n - 1.
The Check passes the day, a made-up day of 3 time blocks, and a made-up day of 24 blocks whose energy so far is exactly half after block 10 (so the answer there is 10).
- Task 4 · interview question
Interview: row_totals.py, forty rows and sixty totals
row_totals(grid) in row_totals.py takes one time block's grid of shape (rows, cols), in watts, and should return each row's total output in watts, as a float array with one total per row: shape (40,) for the plant's (40, 60) grid, (3,) for a grid of 3 rows by 5.
Rukmini's report has 60 totals for 40 rows, and they are not the rows' totals. Fix the function. The Check passes the noon grid, a grid of 3 rows by 5, and a made-up grid of 40 by 60.
Imran will ask: axis=0 or axis=1 for each row's total, and what does the shape of the answer tell you?
What you will learn
- axis=: sum, mean, min, max, std and np.ptp along one axis; the axis you name disappears from the shape.
- Several axes at once: axis=(1, 2) for one number per block.
- keepdims=True: the axis stays, with length 1.
- argmax and argmin with and without an axis, and np.unravel_index for the row and column.
- prod, and any and all along an axis.
- cumsum, cumprod and np.diff: running totals, running products, and the change from one element to the next.
- Energy from power: watts times 0.25 h, divided by 1000, is kWh.
More in Shapes and maths
Build "Aggregation and running totals" 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.