Gabriel - 1 month ago 5x

Python Question

I have a somewhat large

`numpy`

`large_array`

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,)
```

Source (Stackoverflow)

Comments