0

I have a boolean numpy array mask of length n. I also have a numpy array a of length <= n, containing numbers ranging from 0 (inclusive) to n-1 (inclusive), and it contains no duplicates. The query I want to compute is np.array([x for x in a if mask[x]]), but I don't think it's the fastest way to do it.

Is there a faster way of doing this in numpy than the way I just wrote?

michaelsnowden
  • 6,031
  • 2
  • 38
  • 83

1 Answers1

1

It looks like the fastest way to do this is simply a[mask[a]]. I wrote a quick test which shows the difference in speed of the two methods depending on the coverage of the mask, p (the number of true items / n).

import timeit
import matplotlib.pyplot as plt
import numpy as np
n = 10000
p = 0.25
slow_times = []
fast_times = []
p_space = np.linspace(0, 1, 100)
for p in p_space:
    mask = np.random.choice([True, False], n, p=[p, 1 - p])
    a = np.arange(n)
    np.random.shuffle(a)
    y = np.array([x for x in a if mask[x]])
    z = a[mask[a]]
    n_test = 100
    t1 = timeit.timeit(lambda: np.array([x for x in a if mask[x]]), number=n_test)
    t2 = timeit.timeit(lambda: a[mask[a]], number=n_test)
    slow_times.append(t1)
    fast_times.append(t2)
plt.plot(p_space, slow_times, label='slow')
plt.plot(p_space, fast_times, label='fast')
plt.xlabel('p (# true items in mask)')
plt.ylabel('time (ms)')
plt.legend()
plt.title('Speed of method vs. coverage of mask')
plt.show()

Which gave me this plot

enter image description here

So this method is a whole lot faster regardless of the coverage of mask.

michaelsnowden
  • 6,031
  • 2
  • 38
  • 83