Reducing Masks to Answers
Introduction
Welcome back to Boolean Arrays and Masks. We’re now on lesson 3, building on the masks we created and used to filter arrays. Sometimes we don’t need to see every matching value; we just need a clear yes-or-no answer. We’ll learn how np.any and np.all turn a whole mask into one True or False value, so we can check a dataset without writing a loop.
Turn a Mask into One Answer with np.any
A comparison such as response_ms > 500 creates a Boolean mask with one result for each request. As you may recall from the earlier lessons, each True marks a position where the condition matches. If our question is whether at least one request took longer than 500 milliseconds, we can pass that mask to np.any. It returns one True value if even a single position matches. As always, we import NumPy as np first; the later snippets in this lesson use the same import.
The comparison checks each response time against 500, and then np.any checks the resulting mask. Here, the time of 610 milliseconds exceeds the limit, so the printed answer is True.
Require Every Element to Pass with np.all
For a different kind of yes-or-no question, we may need every value to meet a condition. np.all returns True only when every position in its Boolean mask is True. One False makes the overall answer False. We can check the same response times to see whether all requests stayed below 1000 milliseconds.
The comparison creates a mask for the condition “under 1000 milliseconds.” All four response times meet it, so np.all returns True. Notice that the condition uses <, meaning that a value exactly equal to 1000 would not pass.
Contrast any and all on the Same Condition
The clearest way to see the difference is to ask both functions about the same mask:
One request was slow, so np.any says True; three were not, so np.all says False.
| Function | Question it answers | True when |
|---|---|---|
np.any(mask) | Is at least one value True? | One or more positions match |
np.all(mask) | Is every value True? | No position is False |
Both reduce a mask to one Boolean answer rather than returning a mask or a filtered array.
Combine any and all with Cross-Array Comparisons
We can also compare two aligned arrays and then ask a yes-or-no question about the resulting mask. As in our earlier work with same-length arrays, each spending value is compared with the budget at the same position. Here, np.any checks whether any day went over budget, while np.all checks whether every day stayed at or under budget.
The first comparison marks days when spending exceeded the budget. At least one day did, so np.any returns True. The second comparison marks days that stayed within budget, including days exactly at the limit. Since not every day passed, np.all returns False.
Conclusion and Next Steps
We’ve learned how to turn Boolean masks into single answers: np.any returns True when at least one position matches, while np.all returns True only when every position matches. Both let us answer questions about a whole array without writing a loop, and they work with masks from comparisons between aligned arrays, too. In the upcoming practices, we’ll check fridge temperatures, test parcel weights against a limit, and compare class attendance with room capacities.
