```
import numpy as np
A = np.array([4, 7, 3, 4, 2, 8])
print(A == 4)
```

Every element of the Array A is tested, if it is equal to 4. The results of these tests are the Boolean elements of the result array.

Of course, it is also possible to check on "<", "<=", ">" and ">=".

```
print(A < 5)
```

It works also for higher dimensions:

```
B = np.array([[42,56,89,65],
[99,88,42,12],
[55,42,17,18]])
print(B>=42)
```

It is a convenient way to threshold images.

```
import numpy as np
A = np.array([
[12, 13, 14, 12, 16, 14, 11, 10, 9],
[11, 14, 12, 15, 15, 16, 10, 12, 11],
[10, 12, 12, 15, 14, 16, 10, 12, 12],
[ 9, 11, 16, 15, 14, 16, 15, 12, 10],
[12, 11, 16, 14, 10, 12, 16, 12, 13],
[10, 15, 16, 14, 14, 14, 16, 15, 12],
[13, 17, 14, 10, 14, 11, 14, 15, 10],
[10, 16, 12, 14, 11, 12, 14, 18, 11],
[10, 19, 12, 14, 11, 12, 14, 18, 10],
[14, 22, 17, 19, 16, 17, 18, 17, 13],
[10, 16, 12, 14, 11, 12, 14, 18, 11],
[10, 16, 12, 14, 11, 12, 14, 18, 11],
[10, 19, 12, 14, 11, 12, 14, 18, 10],
[14, 22, 12, 14, 11, 12, 14, 17, 13],
[10, 16, 12, 14, 11, 12, 14, 18, 11]])
B = A < 15
B.astype(np.int)
```

If you have a close look at the previous output, you will see, that it the upper case 'A' is hidden in the array B.

We will index an array C in the following example by using a Boolean mask. It is called fancy indexing, if arrays are indexed by using boolean or integer arrays (masks). The result will be a copy and not a view.

In our next example, we will use the Boolean mask of one array to select the corresponding elements of another array. The new array R contains all the elements of C where the corresponding value of (A<=5) is True.

```
C = np.array([123,188,190,99,77,88,100])
A = np.array([4,7,2,8,6,9,5])
R = C[A<=5]
print(R)
```

In the following example, we will index with an integer array:

```
C[[0, 2, 3, 1, 4, 1]]
```

Indices can appear in every order and multiple times!

Extract from the array np.array([3,4,6,10,24,89,45,43,46,99,100]) with Boolean masking all the number

which are not divisible by 3

which are divisible by 5

which are divisible by 3 and 5

which are divisible by 3 and set them to 42

```
import numpy as np
A = np.array([3,4,6,10,24,89,45,43,46,99,100])
div3 = A[A%3!=0]
print("Elements of A not divisible by 3:")
print(div3)
div5 = A[A%5==0]
print("Elements of A divisible by 5:")
print(div5)
print("Elements of A, which are divisible by 3 and 5:")
print(A[(A%3==0) & (A%5==0)])
print("------------------")
#
A[A%3==0] = 42
print("""New values of A after setting the elements of A,
which are divisible by 3, to 42:""")
print(A)
```

### nonzero and where

There is an ndarray method called nonzero and a numpy method with this name. The two functions are equivalent.

For an ndarray a both numpy.nonzero(a) and a.nonzero() return the indices of the elements of a that are non-zero. The indices are returned as a tuple of arrays, one for each dimension of 'a'. The corresponding non-zero values can be obtained with:

`a[numpy.nonzero(a)]`

```
import numpy as np
a = np.array([[0, 2, 3, 0, 1],
[1, 0, 0, 7, 0],
[5, 0, 0, 1, 0]])
print(a.nonzero())
```

If you want to group the indices by element, you can use transpose:

`transpose(nonzero(a))`

A two-dimensional array is returned. Every row corresponds to a non-zero element.

```
np.transpose(a.nonzero())
```

The corresponding non-zero values can be retrieved with:

```
a[a.nonzero()]
```

The function 'nonzero' can be used to obtain the indices of an array, where a condition is True. In the following script, we create the Boolean array B >= 42:

```
B = np.array([[42,56,89,65],
[99,88,42,12],
[55,42,17,18]])
print(B >= 42)
```

np.nonzero(B >= 42) yields the indices of the B where the condition is true:

Calculate the prime numbers between 0 and 100 by using a Boolean array.

Solution:

```
import numpy as np
is_prime = np.ones((100,), dtype=bool)
# Cross out 0 and 1 which are not primes:
is_prime[:2] = 0
# cross out its higher multiples (sieve of Eratosthenes):
nmax = int(np.sqrt(len(is_prime)))
for i in range(2, nmax):
is_prime[2*i::i] = False
print(np.nonzero(is_prime))
```

similar functions:

flatnonzero :

`Return indices that are non-zero in the flattened version of the input array.`

count_nonzero :

`Counts the number of non-zero elements in the input array.`