[Numpy-discussion] Boolean arrays

Ken Watford kwatford+scipy at gmail.com
Fri Aug 27 16:32:14 EDT 2010


On Fri, Aug 27, 2010 at 4:17 PM, Robert Kern <robert.kern at gmail.com> wrote:
> On Fri, Aug 27, 2010 at 15:10, Ken Watford <kwatford+scipy at gmail.com> wrote:
>> On Fri, Aug 27, 2010 at 3:58 PM, Brett Olsen <brett.olsen at gmail.com> wrote:
>>> Hello,
>>>
>>> I have an array of non-numeric data, and I want to create a boolean
>>> array denoting whether each element in this array is a "valid" value
>>> or not.  This is straightforward if there's only one possible valid
>>> value:
>>>>>> import numpy as N
>>>>>> ar = N.array(("a", "b", "c", "b", "b", "a", "d", "c", "a"))
>>>>>> ar == "a"
>>> array([ True, False, False, False, False,  True, False, False,  True],
>>> dtype=bool)
>>>
>>> If there's multiple possible valid values, I've come up with a couple
>>> possible methods, but they all seem to be inefficient or kludges:
>>>>>> valid = N.array(("a", "c"))
>>>>>> (ar == valid[0]) | (ar == valid[1])
>>> array([ True, False,  True, False, False,  True, False,  True,  True],
>>> dtype=bool)
>>>>>> N.array(map(lambda x: x in valid, ar))
>>> array([ True, False,  True, False, False,  True, False,  True,  True],
>>> dtype=bool)
>>>
>>> Is there a numpy-appropriate way to do this?
>>>
>>> Thanks,
>>> Brett Olsen
>>
>> amap: Like Map, but for arrays.
>>
>>>>> ar = numpy.array(("a", "b", "c", "b", "b", "a", "d", "c", "a"))
>>>>> valid = ('a', 'c')
>>>>> numpy.amap(lambda x: x in valid, ar)
>> array([ True, False,  True, False, False,  True, False,  True,  True],
>> dtype=bool)
>
> I'm not sure what version of numpy this would be in; I've never seen it.

Ah, my fault. I started ipython in pylab mode, expected to find
something like "amap", found it, and assumed it was in numpy. It's
actually in matplotlib.mlab, strangely enough.



More information about the NumPy-Discussion mailing list