- We can compare arrays with scalars and other arrays using Pythons standard comparison operators
- There are two different boolean representations of an array. arr.all is True if all elements are True, arr.any is True if any element is True.
- To perform logical functions we need the special NumPy functions np.logical_and, np.logical_or, np.logical_not, np.logical_xor
Introduction
Logic functions allow us to check if logical statements about our arrays are true or false. Luckily, logic functions are very consistent with other array functions. They are performed element-wise by default and they can be performed on specific axes.
Comparing Arrays and Scalars
The same way we can use the arithmetic operators we can use all the logical operators: >, >=, <,< <=, ==, !=,
import numpy as np
arr = np.array([-1, 0, 1])
arr > 0
# array([False, False, True])
arr >= 0
array([False, True, True])
arr < 0
# array([ True, False, False])
arr <= 0
# array([ True, True, False])
arr == 0
# array([False, True, False])
arr != 0
# array([ True, False, True])
Comparing Arrays with Arrays
The result of a logic operation is a boolean array containing binary values of True or False. This particular case shows us logic operations of arrays with scalars. All boolean operations are performed element-wise, so each element is compared against the scalar. We can also compare arrays to arrays if their shape allows it.
arr_one = np.array([-1, 0, 1])
arr_two = np.array([1, 0, -1])
arr_one > arr_two
# array([False, False, True])
arr_one >= arr_two
# array([False, True, True])
arr_one < arr_two
# array([ True, False, False])
arr_one <= arr_two
# array([ True, True, False])
arr_one == arr_two
# array([False, True, False])
arr_one != arr_two
array([ True, False, True])
Truth Value of Arrays and Elements
Sometimes we need to check if an array contains any elements that are considered True in a boolean context. While the boolean value of array elements is well defined, the truth value of an entire array is not defined.
arr = np.array([0, 1])
bool(arr[0])
# False
bool(arr[1])
# True
bool(arr)
# ValueError: The truth value of an array with more
# than one element is ambiguous. Use a.any() or a.all()
NumPy error messages are great. This one is so great that it even tells us which method we need to use to get at the truth value of an array. We can use arr.any() to find out if any of the elements evaluate to True or arr.all() to find out if all elements are True
arr_one = np.array([0, 0])
arr_one.any()
# False
arr_one.all()
# False
arr_two = np.array([0, 1])
arr_two.any()
# True
arr_two.all()
# False
arr_three = np.array([1, 1])
arr_three.any()
# True
arr_three.any()
# True
This can be useful to find out whether an array is empty
arr = np.array([])
arr.any()
# False
Logical Operations
Finally, we need to look at four more logical operations: and, or, not & xor.
Unfortunately we can’t just use the Python keywords. The reason is in the error message above: “ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()”. It is ambiguous because NumPy does not know if we want to perform the operation element-wise or if we want to perform the operation on the truth value of the array. NumPy does not try to guess which one we mean, so it throws the error. To get these logical functions we need to call some more explicit NumPy functions.
arr_one = np.array([0,1,1])
arr_two = np.array([0,0,1])
np.logical_and(arr_one, arr_two)
# array([False, False, True])
np.logical_or(arr_one, arr_two)
# array([False, True, True])
np.logical_not(arr_one)
# array([ True, False, False])
np.logical_xor(arr_one, arr_two)
# array([False, True, False])
Summary
We are now well equipped to deal with arrays. We can compare arrays with scalar values and other arrays using the the standard comparison operators. We can also perform logical operations on arrays with the special NumPy functions (logical_and, logical_or, logical_not and logcal_xor). Finally we can get two different boolean values of an arrays using arr.all and arr.any.