5

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?

3
  • 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() Commented Apr 30, 2016 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? Commented Apr 30, 2016 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. Commented Apr 30, 2016 at 23:27

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

d = [[1,2,3], [1,3,4],[4,5,6]] >>> d.index([4,5,6]) 2
This is okay, so the problem is only with a list of numpy arrays?
In other words, if I have a list of numpy arrays, I just can't do .index()?
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.