Select by Condition
Build a Boolean mask from a numerical condition and use it to select matching values or complete observations.
Position is useful when we already know where a value sits. A different question is, “Which readings are at least 22 degrees?” The answer depends on the values, so we begin with a condition rather than a row or column number.
The array has shape (3, 2): three observations by two sensors.
A Comparison Produces a Boolean Array
Compare the whole array with one threshold:
[[False False]
[False True]
[False True]]
(3, 2)
NumPy performs the comparison at every position. The resulting boolean array,
often called a mask, has the same shape as readings. Each True marks a
position whose value meets the condition. Here those positions contain 22.0
and 24.0.
The condition includes equality. If the question had said “above 22,” we would
use readings > 22.0, and 22.0 would no longer be selected. The operator is
part of the data question, not a minor spelling choice.
Q1. Predict a mask
What boolean values does readings[:, 0] < 20.0 produce, in observation order?
Select one choice, then check.
HintWrite the sensor values first
readings[:, 0] is [18.5, 21.5, 19.0]. Apply < 20.0 to each value.
SolutionCompare at each observation
The mask is [True, False, True].
Use the Mask to Select Values
Place a same-shaped boolean mask inside the brackets:
[22. 24.]
(2,)
NumPy collects the values at True positions into a one-dimensional result.
The source was a table, but this question asks for individual readings rather
than complete observations. The result therefore has shape (2,), not
(3, 2).
Boolean selection produces a result independent of the source array. We will use that fact here, but Lesson 8 will establish the storage rule with direct evidence and compare it with slices and transpose. For now, the important point is the selection rule: one boolean decision corresponds to each source position.
Q2. Select values that meet a condition
Complete the condition so cool_readings contains every reading below
20.0.
Editable Python
Ready to run.
HintCompare, then select
Use readings < 20.0 to make the mask. Then place cool_mask inside the
selection brackets.
SolutionUse one table-shaped mask
The mask has shape (3, 2). It selects 18.5 and 19.0 into a result of
shape (2,).
Select Complete Rows with One Stated Rule
Sometimes the question is about observations rather than individual values.
Suppose we need every observation where the second sensor reports at least
22.0. First derive one boolean value per observation:
[False True True]
(3,)
This mask has shape (3,) because it contains one decision for each
observation. Use it on axis 0 to keep complete rows:
[[21.5 22. ]
[19. 24. ]]
(2, 2)
The condition names the second sensor, but the selection keeps both sensors for every accepted observation. This distinction matters when several arrays store aligned information about the same rows: derive one row mask, then apply that same decision to each aligned array.
Q3. Build and apply a row mask
Select every complete observation whose first sensor is below 20.0.
Editable Python
Ready to run.
HintMake one decision for each row
readings[:, 0] selects the first sensor across all observations. Compare it
with 20.0, then place the resulting mask inside readings[...].
SolutionUse the sensor condition as a row selector
The mask is [True, False, True]. It keeps the first and last observations,
so the result has shape (2, 2).
A comparison produces a boolean array. A table-shaped mask selects individual values, while a one-decision-per-observation mask can retain complete rows. The next lesson arranges the selected values into an intentional layout.