NumPy · Chapter 9 of 18

Broadcasting

By the end of this chapter you can do arithmetic between arrays of different shapes: say whether two shapes fit by lining them up from the right, give every block, row, column or panel its own value with no loop, turn a 1-D array into a row or a column with np.newaxis, keep an aggregate's axes with keepdims so it lines up again, build a whole table of combinations from two short arrays, and read the error when shapes do not fit. 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

The panels at DhoopGrid are not all the same. Blocks A and B have 540 W panels from 2023, block C has 550 W panels and block D 555 W, from 2024. Rukmini wants every reading of the day as a percent of its own panel's rating, because 460 W is a good noon for a 540 W panel and a slightly weaker one for a 555 W panel. Meenal wants revenue tables for prices she does not know yet, and Bhanwar wants to see what each row would make after he cleans it.

The day is shape (96, 40, 60). The ratings, read from panels.csv, are one grid of shape (40, 60). So far you can do arithmetic with one number, or between two arrays of exactly the same shape. Neither fits: you would loop over the 96 blocks and divide each (40, 60) grid by the ratings, and for a value per row you would loop over 40 rows. For a table of every block at every price you would write two nested loops. NumPy has one rule that does all of these in one line, with no copies: broadcasting.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    percent.py: every panel against its own rating

    Write percent_of_peak(power, peak) in percent.py. power is the day's readings in watts, shape (96, 40, 60), or one block of shape (40, 60); peak is each panel's rating in watts, shape (40, 60) (540, 550 or 555 W, from panels.csv). Return every reading as a percent of its own panel's rating: the watts divided by the rating, times 100, as a float array of the same shape as power. A panel making 270 W of its 540 W is 50.0.

    The Check calls it with the day and the ratings, with the noon block and the ratings, and with 2 blocks of a small plant of 2 rows of 3 panels and their (2, 3) ratings, so nothing may depend on 96, 40 or 60. Use no Python loop: a for, a while or a comprehension in percent_of_peak fails the task.

  2. Task 2

    block_gap.py: every panel against its block's average

    Write gap_from_mean(day) in block_gap.py. day has shape (blocks, rows, cols), in watts. For every block, work out the mean of all its panels (one number a block), and return every reading minus its own block's mean: the watts above (positive) or below (negative) the plant's average at that time, as a float array of the same shape as day. In a block whose panels read 450, 450 and 0 W, the mean is 300 and the gaps are 150, 150 and -300.

    One mean for the whole day is not right, and neither is a mean per row. The Check calls it with the day, shape (96, 40, 60), and with 3 blocks of a small plant of 2 rows of 3 panels, shape (3, 2, 3), one of them all zeros (night). Use no Python loop.

  3. Task 3

    scenarios.py: revenue at every price

    Write revenue_table(kwh, prices) in scenarios.py. kwh is each block's energy, a 1-D array of n blocks in kWh; prices is a 1-D array of k possible prices in rupees a MWh. Return the revenue in rupees for every block at every price as a float array of shape (n, k): row i, column j is block i's kWh times price j, divided by 1000 (a MWh is 1000 kWh). Blocks go down the rows and prices across the columns.

    The Check calls it with the day's 96 blocks and 3 prices (shape (96, 3)), with 4 blocks and 4 prices (so a table the wrong way round fails), and with 2 blocks and 1 price. Use no Python loop.

  4. Task 4 · interview question

    Interview: cleaning.py, a gain for every row

    Bhanwar's after_cleaning(grid, gain) takes a grid of panels at noon, shape (rows, cols) in watts, and gain, a 1-D array of one number per row (1.22 means 22 % more after cleaning). It should return every panel's watts times its own row's gain: row r times gain[r], a float array of shape (rows, cols). On the plant, a (40, 60) grid with 40 gains, it stops with ValueError: operands could not be broadcast together with shapes (40,60) (40,); on a square grid of 3 rows of 3 it runs but gives wrong numbers. Fix it.

    The Check calls it with the plant at noon and 40 gains, with a square (3, 3) grid and 3 gains, and with one row of 5 panels and 1 gain. Use no Python loop.

    Imran will ask: why does (40, 60) with (40,) fail when (40, 60) with (40, 1) works? And what went wrong on the square grid?

What you will learn

  • Broadcasting: arithmetic between arrays of different shapes, when the shapes fit.
  • The rule: line the shapes up from the right; each pair of sizes is equal or one of them is 1; a missing axis counts as 1.
  • A value per block, row, column or panel: shapes (96, 1, 1), (40, 1), (60,) and (40, 60) against the day.
  • Nothing is copied: np.broadcast_to and a stride of 0.
  • np.newaxis for a column or a row, and outer tables from two 1-D arrays.
  • keepdims=True so an aggregate broadcasts back.
  • Reading the error, and np.broadcast_shapes to ask before you try.

More in Shapes and maths

Build "Broadcasting" 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.