[Numpy-discussion] a==b for numpy arrays

Abel Daniel abli at freemail.hu
Mon Dec 11 12:51:29 EST 2006


>
Hi!

My unittests got broken because 'a==b' for numpy arrays returns an
array instead of returning True or False:

>>> import numpy
>>> a = numpy.array([1, 2])
>>> b = numpy.array([1, 4])
>>> a==b
array([True, False], dtype=bool)

This means, for example:
>>> if a==b:
...   print 'equal'
...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: The truth value of an array with more than one element is ambiguous.
Use a.any() or a.all()
>>>


Now, I think that having a way of getting an element-wise comparison
(i.e. getting an array of bools) is great. _But_ why make that the
result of a '==' comparison? Is there any actual code that does, for
example
>>> result_array = a==b
or any variant thereof?

Thanks in advance,
Daniel





More information about the NumPy-Discussion mailing list