How do I return a nonflat numpy array selecting elements given a set of conditions?
<p>I have a multidimensional array, say of shape (4, 3) that looks like</p>

<pre><code>a = np.array([(1,2,3),(4,5,6),(7,8,9),(10,11,12)])
</code></pre>

<p>If I have a list of fixed conditions</p>

<pre><code>conditions = [True, False, False, True]
</code></pre>

<p>How can I return the list</p>

<pre><code>array([(1,2,3),(10,11,12)])
</code></pre>

<p>Using <code>np.extract</code> returns</p>

<pre><code>&gt;&gt;&gt; np.extract(conditions, a)
array([1, 4])
</code></pre>

<p>which only returns the first element along each nested array, as opposed to the array itself. I wasn't sure if or how I could do this with <code>np.where</code>. Any help is much appreciated, thanks!</p>

<p>Let's define you variables:</p>

<pre><code>&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; a = np.array([(1,2,3),(4,5,6),(7,8,9),(10,11,12)])
&gt;&gt;&gt; conditions = [True, False, False, True]
</code></pre>

<p>Now, let's select the elements that you want:</p>

<pre><code>&gt;&gt;&gt; a[np.array(conditions)]
array([[ 1,  2,  3],
       [10, 11, 12]])
</code></pre>

<h3>Aside</h3>

<p>Note that the simpler <code>a[conditions]</code> has some ambiguity:</p>

<pre><code>&gt;&gt;&gt; a[conditions]
-c:1: FutureWarning: in the future, boolean array-likes will be handled as a boolean array index
array([[4, 5, 6],
       [1, 2, 3],
       [1, 2, 3],
       [4, 5, 6]])
</code></pre>

<p>As you can see, <code>conditions</code> are treated here as (integer-like) index values which is not what we wanted.</p>

python arrays numpy conditional slice