Boolean Indexing with Numpy

In the previous notes it was given that boolean array filters out false values.

Now in the example
a2 = np.array([1, 2, 3, 4, 5])
a2_bool = a2 > 2
a2[a2_bool] = 99
how will print(a2) prints [1 2 99 99 99] instead of [99 99 99] since first two values were false?

If you want to print array([ 1, 2, 99, 99, 99]), then do the following:

import numpy as np
a2 = np.array([1, 2, 3, 4, 5])
>>> a2
array([1, 2, 3, 4, 5])

a2_bool = a2 > 2
>>> a2_bool
array([False, False,  True,  True,  True])

>>> a2[a2_bool]
array([3, 4, 5])

a2[a2_bool] = 99
>>> a2[a2_bool] 
array([99, 99, 99])

>>> a2
array([ 1,  2, 99, 99, 99])

If you want to print array([99, 99, 99]), then do the following:

import numpy as np
a2 = np.array([1, 2, 3, 4, 5])
>>> a2
array([1, 2, 3, 4, 5])

a2_bool = a2 > 2
>>> a2_bool
array([False, False,  True,  True,  True])

>>> a2[a2_bool]
array([3, 4, 5])

a2[a2_bool] = 99
>>> a2[a2_bool] 
array([99, 99, 99])