[Numpy-discussion] Extract subset from an array

Francesc Alted faltet@pytables....
Wed Feb 17 02:09:22 CST 2010


A Wednesday 17 February 2010 08:34:06 Nicola Creati escrigué:
> Any kind of improvement is really appreciated.

Well, if you cannot really transpose your matrix, numexpr can also serve as a 
good accelerator:

In [1]: import numpy as np

In [2]: import numexpr as ne

In [3]: x_min, x_max, y_min, y_max = .3, .5, .4, .7

In [4]: array = np.random.random((10000000, 3))

In [5]: time (array[:,0]>x_min) & (array[:,0]<x_max) & (array[:,1]>y_min) & 
(array[:,1]<y_max)
CPU times: user 0.23 s, sys: 0.03 s, total: 0.26 s
Wall time: 0.27 s
Out[6]: array([False, False, False, ..., False, False, False], dtype=bool)

In [9]: time ne.evaluate("(a>x_min) & (a<x_max) & (b>y_min) & (b<y_max)", 
{'a': array[:,0], 'b': array[:,1]})
CPU times: user 0.16 s, sys: 0.00 s, total: 0.16 s
Wall time: 0.16 s
Out[10]: array([False, False, False, ..., False, False, False], dtype=bool)

Again, an 1.7x of improvement, but without the need for transposing.

-- 
Francesc Alted


More information about the NumPy-Discussion mailing list