The truth value of an array with more than one element is ambigous when trying to index an array The truth value of an array with more than one element is ambigous when trying to index an array numpy numpy

The truth value of an array with more than one element is ambigous when trying to index an array


As I told you in a comment to a previous answer, you need to use either:

c[a & b]

or

c[np.logical_and(a, b)] 

The reason is that the and keyword is used by Python to test between two booleans. How can an array be a boolean? If 75% of its items are True, is it True or False? Therefore, numpy refuses to compare the two.

So, you either have to use the logical function to compare two boolean arrays on an element-by-element basis (np.logical_and) or the binary operator &.

Moreover, for indexing purposes, you really need a boolean array with the same size as the array you're indexing. And it has to be an array, you cannot use a list of True/False instead: The reason is that using a boolean array tells NumPy which element to return. If you use a list of True/False, NumPy will interpret that as a list of 1/0 as integers, that is, indices, meaning that you' either get the second or first element of your array. Not what you want.

Now, as you can guess, if you want to use two boolean arrays a or b for indexing, choosing the items for which either a or b is True, you'd use

c[np.logical_or(a,b)]

or

c[a | b]


You usually get this error message when trying to use Python boolean operators (not, and, or) on comparison expressions involving Numpy arrays, e.g.

>>> x = np.arange(-5, 5)>>> (x > -2) and (x < 2)Traceback (most recent call last):  File "<ipython-input-6-475a0a26e11c>", line 1, in <module>    (x > -2) and (x < 2)ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

That's because such comparisons, as opposed to other comparisons in Python, create arrays of booleans rather than single booleans (but maybe you already knew that):

>>> x > -2array([False, False, False, False,  True,  True,  True,  True,  True,  True], dtype=bool)>>> x < 2array([ True,  True,  True,  True,  True,  True,  True, False, False, False], dtype=bool)

Part of the solution to your problem probably to replace and by np.logical_and, which broadcasts the AND operation over two arrays of np.bool.

>>> np.logical_and(x > -2, x < 2)array([False, False, False, False,  True,  True,  True, False, False, False], dtype=bool)>>> x[np.logical_and(x > -2, x < 2)]array([-1,  0,  1])

However, such arrays of booleans cannot be used to index into ordinary Python lists, so you need to convert that to an array:

rbs = np.array([ish[4] for ish in realbooks])


# draw the previous original bounding boxesif rectsPersist != None:    for (x, y, w, h) in rectsPersist:        cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2)# draw the previous final bounding boxesif pickPersist != None:    for (xA, yA, xB, yB) in pickPersist:        cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)if not count % 1:    count = 0    image = imutils.resize(image, width=min(700, image.shape[1]))

if rectsPersist != None:ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()