title: Filtering (reducing) a NumPy Array
body: <p>Suppose I have a NumPy array <code>arr</code> that I want to element-wise filter (reduce) depending on the truth value of a (broadcastable) function, e.g.
  I want to get only values below a certain threshold value <code>k</code>:</p>
  <pre class="lang-py prettyprint-override"><code>def cond(x):
      return x &lt; k
  </code></pre>
  <p>There are a couple of methods, e.g.:</p>
  <ol>
  <li>Using a generator: <code>np.fromiter((x for x in arr if cond(x)), dtype=arr.dtype)</code> (which is a memory efficient version of using a list comprehension: <code>np.array([x for x in arr if cond(x)])</code> because <a href="https://numpy.org/doc/stable/reference/generated/numpy.fromiter.html" rel="nofollow noreferrer"><code>np.fromiter()</code></a> will produce a NumPy array directly, without the need to allocate an intermediate Python <code>list</code>)</li>
  <li>Using boolean masking: <code>arr[cond(arr)]</code></li>
  <li>Using integer indexing: <code>arr[np.nonzero(cond(arr))]</code> (or equivalently using <a href="https://numpy.org/doc/stable/reference/generated/numpy.where.html" rel="nofollow noreferrer"><code>np.where()</code></a> as it defaults to <a href="https://numpy.org/doc/stable/reference/generated/numpy.nonzero.html" rel="nofollow noreferrer"><code>np.nonzero()</code></a> with only one condition)</li>
  <li>Using explicit looping with:
  <ul>
  <li>single pass and final copying/resizing</li>
  <li>two passes: one to determine the size of the result and one to actually perform the computation</li>
  </ul>
  </li>
  </ol>
  <p>(The last two approaches to be accelerated with <a href="https://cython.org/" rel="nofollow noreferrer">Cython</a> or <a href="https://numba.pydata.org/" rel="nofollow noreferrer">Numba</a>)</p>
  <p>Which is the fastest? What about memory efficiency?</p>
  <hr />
  <p>(EDITED: To use directly <code>np.nonzero()</code> instead of <code>np.where()</code> as per  @ShadowRanger comment)</p>