I need to filter an array to remove elements that are below a certain threshold. My current code is as follows:
threshold = 5 a = numpy.array(range(10))
The problem is that this creates a temporary list using a filter with a lambda function (slow).
Since this is a fairly simple operation, there may be a numpy function that does this in an efficient way, but I could not find it.
I thought that another way to achieve this could be to sort the array, search for the threshold index and move the slice from this index forward, but even if it is faster for small inputs (and it will not be noticeable in any case), it is finally asymptotically less efficient as input size increases.
Any ideas? Thank!
Update . I also made some measurements, and sorting + slicing was twice as fast as a clean python filter when the input was 100,000,000 records.
In [321]: r = numpy.random.uniform(0, 1, 100000000) In [322]: %timeit test1(r)
python filter numpy threshold
fortran Nov 03 '11 at 11:52 2011-11-03 11:52
source share