Boolean arrays are useful for advanced array indexing (also see Section 5.3.1: Indexing with Boolean arrays). A Boolean array is simply an array for which the entries have the type bool:
A = array([True,False]) # Boolean array A.dtype # dtype('bool')
Any comparison operator acting on arrays will create a Boolean array instead of a simple Boolean:
M = array([[2, 3], [1, 4]]) M > 2 # array([[False, True], # [False, True]]) M == 0 # array([[False, False], # [False, False]]) N = array([[2, 3], [0, 0]]) M == N # array([[True, True], # [False, False]])
Note that because array comparison creates Boolean arrays, one cannot use array comparison directly in conditional statements, for example, if statements. The solution is to use the methods all and any to create a simple True or False:
A = array([[1,2],[3,4]]) B = array([[1,2],[3,3]]) A == B # creates array([[True...