Array Comparisons¶

Unlike lists, we can perform numerical comparisons with arrays. The comparison is carried out for each element of the array, and the result is an array of boolean values, containing the results of each comparison.

import numpy as np
someArray = np.array([4, 7, 6, 3, 9, 8])

print(someArray < 5)
[ True False False  True False False]

There are many applications of array comparisons, but one is that it provides us with a convenient way to count the number of elements in an array that satisfy a certain condition. Since Python treats True as being equal to 1 and False as being equal to 0, we can use the sum function along with Boolean masking to count the number of elements in an array that satisfy a certain critera.

cat = np.array(['A', 'C', 'A', 'B', 'B', 'C', 'A', 'A' ,
                'C', 'B', 'C', 'C', 'A', 'B', 'A', 'A'])
print('Count of A:', np.sum(cat == 'A'))
print('Count of B:', np.sum(cat == 'B'))
print('Count of C:', np.sum(cat == 'C'))
Count of A: 7
Count of B: 4
Count of C: 5
val = np.array([8, 1, 3, 6, 10, 6, 12, 4,
                6, 1, 4, 8,  5, 4, 12, 4])

print('Number of elememts > 6: ', np.sum(val > 6) )
print('Number of elements <= 6:', np.sum(val <= 6) )
print('Number of even elements:', np.sum(val % 2 == 0) )
print('Number of odd elements: ', np.sum(val % 2 != 0) )
Number of elememts > 6:  5
Number of elements <= 6: 11
Number of even elements: 12
Number of odd elements:  4

Logical Operators for Arrays¶

We can use the & and | operators to combine two boolean arrays into a single boolean array. Numpy also provides a unary negation operator ~.

  • & performs the and operation on the elements of the two arrays, one pair at a time.

  • | performs the or operation on the elements of the two arrays, one pair at a time.

  • ~ can be placed in front of a boolean array to negate the elements of that array.

We will illustrate how these operators work using the two boolean arrays defined below.

b1 = np.array([True, True, False, False])
b2 = np.array([True, False, True, False])

In the cell below, we illustrate the use of the & and | operators.

print('b1 and b2:', b1 & b2)
print('b1 or b2: ', b1 | b2)
b1 and b2: [ True False False False]
b1 or b2:  [ True  True  True False]

We will now provide example of using the negation operator ~.

print('Negation of b1:', ~b1)
print('Negation of b2:', ~b2)
Negation of b1: [False False  True  True]
Negation of b2: [False  True False  True]

We can use these operators to perform counts that depend on two (or more) conditions.

In the cell below, we use comparisons to count the number of elements in val that are even and greater than 5.

print(np.sum((val % 2 == 0) & (val > 5)))
8

The following cell uses three comparisons to count the number of elements in val that are even, divisible by 3, and greater than 7.

print(np.sum((val % 2 == 0) & (val % 3 == 0) & (val > 7)))
2

We can use the logical operators on boolean arrays produced by comparisons involving different lists. In the cell below, we count the number of elements in cat that are equal to A, and for which the associated element of val is greater than 5.

print(np.sum( (cat == 'A') & (val > 5) ))
3