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>You could do -</p>

<pre><code>a[~(a&lt;0).any(axis=(1,2))]
</code></pre>

<p>Or the equivalent with <code>.all()</code> and thus avoid the <code>inverting</code> -</p>

<pre><code>a[(a&gt;=0).all(axis=(1,2))]
</code></pre>

<p>Sample run -</p>

<pre><code>In [35]: a
Out[35]: 
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]]])

In [36]: a[~(a&lt;0).any(axis=(1,2))]
Out[36]: 
array([[[0, 2, 0],
        [3, 1, 1]],

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

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

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

python arrays numpy multidimensional-array vectorization