NumPy · Chapter 5 of 18
Masks and where
By the end of this chapter you can ask a question of every element at once and get a bool array (a mask) back, pick, count and change the elements where it is True, combine questions with &, | and ~, add up a whole array with .sum(), .mean(), .max() and .min(), and choose between values with np.where, np.select and np.clip. 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
Every evening Rukmini asks the same kind of question: how many panels made less than they should, and what share of the plant is that? Bhanwar wants each panel on his board as a code, dead, weak or ok, so he knows where to walk first. And the loggers glitch: a reading of -2.5 W, or 9,999 W from a panel rated 550, which nobody may add into a total.
Every one of these is a test on each reading. With what you know so far you would write a loop over 40 rows and 60 columns with an if inside: 2,400 Python steps for one block, 230,400 for the day, and a new loop for every new question. NumPy asks the question of every element in one step, in C, and gives the answers back as an array you can count with, pick with, and write through.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
low_noon.py: the panels under a limit
Write three functions in low_noon.py. grid is an array of readings in watts, of any shape, and limit is a number. "Under" means less than: a reading equal to limit is not under.
count_low(grid, limit) returns how many readings are under limit, as a whole number.
share_low(grid, limit) returns the share of readings under limit, a float from 0 to 1 (0.25 is a quarter of them).
low_readings(grid, limit) returns the readings under limit as a 1-D array of floats, in the grid's order, row by row.
The Check tries the noon grid with 300, the grid at 14:30 with 200, a (2, 3) grid with readings on the limit, and the noon grid with a limit of 0, which nothing is under: then the answers are 0, 0.0 and an empty array.
- Task 2
status.py: a status code for every panel
Write status(grid, dead_below, weak_below) in status.py. It returns an array of whole numbers of the same shape as grid: 0 (dead) for a reading under dead_below, 1 (weak) for a reading from dead_below up to, not including, weak_below, and 2 (ok) for a reading of weak_below or more. For [0.0, 120.5, 455.8] with 1.0 and 400.0 it is [0 1 2].
The Check tries the noon grid with 1.0 and 400.0, the grid at 14:30 with 50.0 and 350.0, and a (2, 4) grid with readings exactly on the thresholds. Floats fail: the codes are whole numbers.
- Task 3
clip_sensor.py: readings that cannot be
Write two functions in clip_sensor.py. readings is an array of watts of any shape; low and high are numbers.
cleaned(readings, low, high) returns a new array of the same shape in which every reading under low becomes low, every reading over high becomes high, and the rest stay as they are. It must leave the readings it was given unchanged.
out_of_range(readings, low, high) returns how many readings are under low or over high, as a whole number. A reading equal to low or high is in range.
The Check tries a grid with five bad readings (-0.8, -2.5, 601, 4000 and 9999) with 0 and 600, a row of 6 with 0 and 560, and the noon grid with 0 and 600, which has none.
- Task 4 · interview question
Interview: in_band.py, and on arrays
Bhanwar's in_band(grid, low, high) should return a mask of the readings from low to high, both ends included, but it returns grid >= low and grid <= high, which stops with ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(). Fix it so it returns a bool array of the same shape as grid, True where a reading is from low to high (both included) and False elsewhere. For [379.9, 380.0, 420.0, 420.1] with 380.0 and 420.0 it is [False True True False].
The Check tries the noon grid with 380.0 and 420.0, the grid at 10:00 with 0.0 and 0.0, and a row with readings on both ends.
Imran will ask: why does "and" fail on arrays, and what does & need that "and" does not?
What you will learn
- Masks: a comparison gives a bool array; a[mask] picks the True elements.
- Whole-array sums: .sum(), .mean(), .max(), .min(), and a mask's sum is a count and its mean a share.
- Combining: &, | and ~, with brackets round each comparison, and why and fails; np.any and np.all.
- Masks over rows, and writing through a mask.
- np.where to choose between two values, and to find positions; np.nonzero and np.argwhere.
- np.select for more than two outcomes, np.clip for a range, and np.putmask.
More in Arrays from zero
Build "Masks and where" 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.