4

centers is a list, [ ], of numpy arrays. shortest_dist[1] is an numpy array. However, when I do:

centers.index(shortest_dist[1])

It tells me

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

This is weird, so I tried the following things:

See the following demo. I cannot make sense of what's happening.

>>> 
>>> 
>>> 
>>> a = np.asarray([1,2,3,4,5])
>>> b = np.asarray([2,3,4,5,6])
>>> c = []
>>> c.append(a)
>>> c.append(b)
>>> c.index(a)
0
>>> c.index(c[0])
0
>>> c.index(c[1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is    ambiguous. Use a.any() or a.all()
>>> c.index(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>> len(c)
2
>>> c[1]
array([2, 3, 4, 5, 6])
>>> b
array([2, 3, 4, 5, 6])
>>> c.index(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>> 

So it's okay to query the index of a, but not b, although both are numpy arrays? Does this have to do when my error, which is mentioned at the beginning of the question?

Jobs
  • 3,317
  • 6
  • 26
  • 52
  • 1
    Possible duplicate of [Numpy: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()](https://stackoverflow.com/questions/35688865/numpy-valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-amb) – pppery Apr 30 '16 at 22:19
  • @ppperry: If this is a possible duplicate of that post, can you tell what the issue is in my demo and code above from reading that post? What's causing the problem and how to correct it? – Jobs Apr 30 '16 at 22:20
  • This `ValueError` means that a boolean array is being used in a context that expects a scalar boolean - an `if` or here a list `index` or `compare`. – hpaulj Apr 30 '16 at 23:27

1 Answers1

7

When you compare arrays, you get an array. Numpy is refusing to interpret the results of those comparisons as a boolean.

>>> c[0] == c[0]
array([ True,  True,  True,  True,  True], dtype=bool)
>>> bool(c[0] == c[0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The implementation of index is checking such comparisons to find the index to return. Presumably it has an optimisation which checks for identity equality first which is why c.index(a) doesn't raise an error. But in c.index(b) it has to check if a == b and that's when the error happens. You can write your own loop or convert all the arrays to lists first.

Alex Hall
  • 34,833
  • 5
  • 57
  • 89
  • d = [[1,2,3], [1,3,4],[4,5,6]] >>> d.index([4,5,6]) 2 – Jobs Apr 30 '16 at 22:25
  • This is okay, so the problem is only with a list of numpy arrays? – Jobs Apr 30 '16 at 22:25
  • 2
    In other words, if I have a list of numpy arrays, I just can't do .index()? – Jobs Apr 30 '16 at 22:25
  • 2
    @Jobs Yes, list comparisons just return booleans. And yes, you can't use `index` on a list of arrays. Really this shows that arrays don't implement `==` correctly since they break the contract. – Alex Hall Apr 30 '16 at 22:27