title: Efficient thresholding filter of an array with numpy
body: <p>I need to filter an array to remove the elements that are lower than a certain threshold. My current code is like this:</p>
  
  <pre><code>threshold = 5
  a = numpy.array(range(10)) # testing data
  b = numpy.array(filter(lambda x: x &gt;= threshold, a))
  </code></pre>
  
  <p>The problem is that this creates a temporary list, using a filter with a lambda function (slow).</p>
  
  <p>As this is a quite simple operation, maybe there is a numpy function that does it in an efficient way, but I've been unable to find it.</p>
  
  <p>I've thought that another way to achieve this could be sorting the array, finding the index of the threshold and returning a slice from that index onwards, but even if this would be faster for small inputs (and it won't be noticeable anyway), its definitively asymptotically less efficient as the input size grows.</p>
  
  <p>Any ideas? Thanks!</p>
  
  <p><strong>Update</strong>: I took some measurements too, and the sorting+slicing was still twice as fast than the pure python filter when the input was 100.000.000 entries.</p>
  
  <pre><code>In [321]: r = numpy.random.uniform(0, 1, 100000000)
  
  In [322]: %timeit test1(r) # filter
  1 loops, best of 3: 21.3 s per loop
  
  In [323]: %timeit test2(r) # sort and slice
  1 loops, best of 3: 11.1 s per loop
  
  In [324]: %timeit test3(r) # boolean indexing
  1 loops, best of 3: 1.26 s per loop
  </code></pre>