numpy array boolean index true false code example
Example 1: python numpy array boolean indexing
x = np.array([1., -1., -2., 3])
x[x < 0] = 20
x
Output:
array([ 1., 20., 20., 3.])
Example 2: numpy boolean array
arr_1 = np.random.randn(3, 3)
arr_2 = np.random.randn(3, 3)
bool_arr = arr_1 < 0.5
print(bool_arr.dtype)
# output: bool
bool_arr = arr_1 < arr_2
print(bool_arr.dtype)
# output: bool