I am trying to create a "mask" numpy.array by specifying certain criteria. Python even has good syntax for something like this:
>> A = numpy.array([1,2,3,4,5]) >> A > 3 array([False, False, False, True, True])
But if I have a list of criteria, not a range:
>> A = numpy.array([1,2,3,4,5]) >> crit = [1,3,5]
I can not do it:
>> A in crit
I need to do something based on a list comprehension, for example:
>> [a in crit for a in A] array([True, False, True, False, True])
It is right.
Now the problem is that I am working with large arrays and the code above is very slow. Is there a more natural way to do this surgery that can speed it up?
EDIT: I was able to get a little acceleration by doing crit in the set.
EDIT2: For those interested:
Jouni approach: 1000, best 3: 102 μs per loop
numpy.in1d: 1000, best of 3: 1.33 ms per cycle
EDIT3: just checked again with B = randint (10, size = 100)
Jouni approach: 1000, best 3: 2.96 ms per cycle
numpy.in1d: 1000, best of 3: 1.34 ms per cycle
Conclusion : use numpy.in1d () if B is not very small.