[Numpy-discussion] Extract subset from an array

Nicola Creati ncreati@inogs...
Wed Feb 17 02:46:29 CST 2010

Francesc Alted wrote:
> 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.
this morning I tried numexpr and I got a good speed improvement as you 
just suggest.
Thanks for keep helping me. :)


Nicola Creati
Istituto Nazionale di Oceanografia e di Geofisica Sperimentale - OGS www.inogs.it Dipartimento di Geofisica della Litosfera Geophysics of Lithosphere Department CARS (Cartography and Remote Sensing) Research Group http://www.inogs.it/Cars/ Borgo Grotta Gigante 42/c 34010 Sgonico - Trieste - ITALY ncreati@ogs.trieste.it
off.   +39 040 2140 213
fax.   +39 040 327307

This communication, that may contain confidential and/or legally privileged information, is intended solely for the use of the intended addressees. Opinions, conclusions and other information contained in this message, that do not relate to the official business of OGS, shall be considered as not given or endorsed by it. Every opinion or advice contained in this communication is subject to the terms and conditions provided by the agreement governing the engagement with such a client. Any use, disclosure, copying or distribution of the contents of this communication by a not-intended recipient or in violation of the purposes of this communication is strictly prohibited and may be unlawful. For Italy only: Ai sensi del D.Lgs.196/2003 - "T.U. sulla Privacy" si precisa che le informazioni contenute in questo messaggio sono riservate ed a uso esclusivo del destinatario.

More information about the NumPy-Discussion mailing list