[Numpy-discussion] Extract subset from an array

Neil Crighton neilcrighton@gmail....
Tue Feb 16 16:17:20 CST 2010


Francesc Alted <faltet <at> pytables.org> writes:

> In [10]: array = np.random.random((3, 10000000))
> 
> then the time drops significantly:
> 
> In [11]: time (array[0]>x_min) & (array[0]<x_max) & (array[1]>y_min) & 
> (array[1]<y_max)
> CPU times: user 0.15 s, sys: 0.01 s, total: 0.16 s
> Wall time: 0.16 s
> Out[12]: array([False, False, False, ..., False, False, False], dtype=bool)
> 
> i.e. walking your arrays row-wise is around 1.7x faster in this case.
> 

It saves some array creation if you use &=:

In [29]: array = np.random.random((10000000, 3))
In [30]: x_min, x_max, y_min, y_max = .3, .5, .4, .7

In [31]: %timeit c = (array[:,0]>x_min) & (array[:,0]<x_max) & 
(array[:,1]>y_min) & (array[:,1]<y_max)
1 loops, best of 3: 633 ms per loop

In [32]: %timeit c = (array[:,0]>x_min); c &= (array[:,0]<x_max); 
c &= (array[:,1]>y_min); c &= (array[:,1]<y_max)
1 loops, best of 3: 604 ms per loop

Only ~5% speedup though, so not a big deal.

Neil




More information about the NumPy-Discussion mailing list