Boolean Masking

Let’s learn about boolean mask arrays and masking operations in Python.

We'll cover the following

Boolean mask arrays

The boolean mask is very useful when it comes to counting, modifying, extracting, or manipulating values in an array based on certain conditions or criteria. For example, the boolean mask can come in handy when we want to count all the values greater than a certain value. We can set a threshold and want to get rid of outliers in our data.

In NumPy, boolean masking is often the most efficient way to accomplish these types of tasks. For example, boolean masking allows us to do the following:

  • We can apply conditions such as >, <, ==, and so on.
  • We can create a mask to filter out the even numbers in a NumPy array.

Let’s look at a very simple example:

Get hands-on with 1400+ tech skills courses.