Element wise comparison in an array of arrays in Numpy
<p>I have the following array of <code>shape(5,2,3)</code>, which is a collection of <code>2 * 3</code> arrays.</p>

<pre><code>a = array([[[ 0,  2,  0],
    [ 3,  1,  1]],

   [[ 1,  1,  0],
    [ 2,  2,  1]],

   [[ 0,  1,  0],
    [ 3,  2,  1]],

   [[-1,  2,  0],
    [ 4,  1,  1]],

   [[ 1,  0,  0],
    [ 2,  3,  1]]])
</code></pre>

<p><strong>1)</strong> How can I check if there exists a <code>2 * 3</code> array in this array of arrays where at least one element is negative in it?</p>

<pre><code>#which is this:
[[-1,  2,  0],
[ 4,  1,  1]]
</code></pre>

<p><strong>2)</strong> After that how can I remove the above found <code>2 * 3</code> array from <code>a</code>?</p>

<p>A vectorized implementation is much appreciated but looping is fine too.</p>

<p>Use <code>any</code>:</p>

<pre><code>In [10]: np.any(a&lt;0,axis=-1)
Out[10]: 
array([[False, False],
       [False, False],
       [False, False],
       [ True, False],
       [False, False]], dtype=bool)
</code></pre>

<p>Or more complete, if you want the corresponding index for (2,3) array:</p>

<pre><code>In [22]: np.where(np.any(a&lt;0,axis=-1).any(axis=-1))
Out[22]: (array([3]),)
# Or as mentioned in comment you can pass a tuple to `any` np.where(np.any(a&lt;0,axis=(1, 2)))
</code></pre>

<p>You can also get the array with a simple indexing:</p>

<pre><code>In [27]: a[np.any(a&lt;0, axis=(1, 2))]
Out[27]: 
array([[[-1,  2,  0],
        [ 4,  1,  1]]])
</code></pre>

python arrays numpy multidimensional-array vectorization