Efficient thresholding filter of an array with numpy Efficient thresholding filter of an array with numpy python python

Efficient thresholding filter of an array with numpy


b = a[a>threshold] this should do

I tested as follows:

import numpy as np, datetime# array of zeros and ones interleavedlrg = np.arange(2).reshape((2,-1)).repeat(1000000,-1).flatten()t0 = datetime.datetime.now()flt = lrg[lrg==0]print datetime.datetime.now() - t0t0 = datetime.datetime.now()flt = np.array(filter(lambda x:x==0, lrg))print datetime.datetime.now() - t0

I got

$ python test.py0:00:00.0280000:00:02.461000

http://docs.scipy.org/doc/numpy/user/basics.indexing.html#boolean-or-mask-index-arrays