Gabriel Gabriel - 3 months ago 8
Python Question

Quickly filter elements in Python array by minimum and maximum values

I have a somewhat large

numpy
array of floats (
large_array
, ~2e7 elements). I need to generate a new array, filtering out all elements beyond certain minimum and maximum values.

I can do this with a simple:

import numpy as np
large_array = np.random.uniform(0., 10000., 20000000)
min_val, max_val = 500., 2700.
arr_f = []
for _ in large_array:
if min_val <= _ <= max_val:
arr_f.append(_)


but it takes a really long time.

How can I speed this up?

Answer

try this:

In [18]: large_array.shape
Out[18]: (20000000,)

In [26]: new = large_array[(large_array >= min_val) & (large_array <= max_val)]

In [27]: new
Out[27]: array([  814.24315891,  1611.53346093,   624.31833231, ...,  1999.08383068,  2212.9825087 ,  1786.08963269])

In [28]: new.shape
Out[28]: (4400475,)