This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 3: Array operations

Boolean indexing

face Josiah Wang

You can also access elements using a boolean np.array as its indices.

This is where boolean expressions (x < 3) become useful! These can be used to filter your np.array. Possibly one of the most useful features that you may end up using a lot!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
>>> y = np.array([[1, 2, 3, 5], [-1, 4, 7, 9]]) 
>>> print(y)
[[ 1  2  3  5]
 [-1  4  7  9]]
>>> print(y < 4)
[[ True  True  True False]
 [ True False False False]]
>>> print(y[y < 4]) # Keep only elements that are <4. The output is a 1D array
[1 2 3 -1]
>>> print(y[(y < 4) & (y > 1)]) # Keep elements that are <4 and >1 
[2 3]
>>> print(y[np.logical_and(y < 4, y > 1)]) # Different way to achieve the above
[2 3]

In Line 10, note the & instead of our usual and in pure Python. This is actually a bitwise and operator (and works for boolean expressions). The and operator is not overloaded for np.array.

Unfortunately you cannot chain 1 < y < 4 either.

Also note the parenthesis surrounding the two expressions (y < 4) and (y > 1). The operator & has higher precedence, so without the parenthesis, it will be interpreted as y < (4 & y) < 1, resulting in an error.

More advanced indexing techniques are covered in the official documentation.