[Numpy-discussion] boolean array indexing and broadcasting rules

Pierre GM pgmdevlist at gmail.com
Fri Jul 29 10:57:43 EDT 2011


On Jul 29, 2011, at 4:07 PM, Mark Wiebe wrote:

> As part of supporting the NA mask, I've rewritten boolean indexing. Here's a timing comparison of my version versus a previous version:
> 
> In [2]: np.__version__
> Out[2]: '1.4.1'
> In [3]: a = np.zeros((1000,1000))
> In [4]: mask = np.random.rand(1000,1000) > 0.5
> In [5]: timeit a[mask] = 1.5
> 10 loops, best of 3: 71.5 ms per loop
> 
> In [2]: np.__version__
> Out[2]: '2.0.0.dev-a1e98d4'
> In [3]: a = np.zeros((1000,1000))
> In [4]: mask = np.random.rand(1000,1000) > 0.5
> In [5]: timeit a[mask] = 1.5
> 100 loops, best of 3: 12.6 ms per loop
> 
> That's a 5.6 times speedup.
> 
> Unfortunately, it turns out that the old code didn't use NumPy broadcasting rules. This change found a bug in the hardmask code of numpy.ma,

Good! And I wouldn't worry too much about that (as nobody really uses hard masks anyway)





More information about the NumPy-Discussion mailing list