Removing elements from an array that are in another array
<p>Say I have these 2D arrays A and B.</p>

<p>How can I remove elements from A that are in B.</p>

<pre><code>A=np.asarray([[1,1,1], [1,1,2], [1,1,3], [1,1,4]])
B=np.asarray([[0,0,0], [1,0,2], [1,0,3], [1,0,4], [1,1,0], [1,1,1], [1,1,4]])
#output = [[1,1,2], [1,1,3]]
</code></pre>

<hr>

<p>To be more precise, I would like to do something like this.</p>

<pre><code>data = some numpy array
label = some numpy array
A = np.argwhere(label==0) #[[1 1 1], [1 1 2], [1 1 3], [1 1 4]]
B = np.argwhere(data&gt;1.5) #[[0 0 0], [1 0 2], [1 0 3], [1 0 4], [1 1 0], [1 1 1], [1 1 4]]
out = np.argwhere(label==0 and data&gt;1.5) #[[1 1 2], [1 1 3]]
</code></pre>

<p>Based on <a href="http://stackoverflow.com/a/38674038/3293881"><code>this solution</code></a> to <a href="http://stackoverflow.com/questions/38674027/find-the-row-indexes-of-several-values-in-a-numpy-array"><code>Find the row indexes of several values in a numpy array</code></a>, here's a NumPy based solution with less memory footprint and could be beneficial when working with large arrays -</p>

<pre><code>dims = np.maximum(B.max(0),A.max(0))+1
out = A[~np.in1d(np.ravel_multi_index(A.T,dims),np.ravel_multi_index(B.T,dims))]
</code></pre>

<p>Sample run -</p>

<pre><code>In [38]: A
Out[38]: 
array([[1, 1, 1],
       [1, 1, 2],
       [1, 1, 3],
       [1, 1, 4]])

In [39]: B
Out[39]: 
array([[0, 0, 0],
       [1, 0, 2],
       [1, 0, 3],
       [1, 0, 4],
       [1, 1, 0],
       [1, 1, 1],
       [1, 1, 4]])

In [40]: out
Out[40]: 
array([[1, 1, 2],
       [1, 1, 3]])
</code></pre>

<p>Runtime test on large arrays -</p>

<pre><code>In [107]: def in1d_approach(A,B):
     ...:     dims = np.maximum(B.max(0),A.max(0))+1
     ...:     return A[~np.in1d(np.ravel_multi_index(A.T,dims),\
     ...:                     np.ravel_multi_index(B.T,dims))]
     ...: 

In [108]: # Setup arrays with B as large array and A contains some of B's rows
     ...: B = np.random.randint(0,9,(1000,3))
     ...: A = np.random.randint(0,9,(100,3))
     ...: A_idx = np.random.choice(np.arange(A.shape[0]),size=10,replace=0)
     ...: B_idx = np.random.choice(np.arange(B.shape[0]),size=10,replace=0)
     ...: A[A_idx] = B[B_idx]
     ...: 
</code></pre>

<p>Timings with <code>broadcasting</code> based solutions -</p>

<pre><code>In [109]: %timeit A[np.all(np.any((A-B[:, None]), axis=2), axis=0)]
100 loops, best of 3: 4.64 ms per loop # @Kasramvd's soln

In [110]: %timeit A[~((A[:,None,:] == B).all(-1)).any(1)]
100 loops, best of 3: 3.66 ms per loop
</code></pre>

<p>Timing with less memory footprint based solution -</p>

<pre><code>In [111]: %timeit in1d_approach(A,B)
1000 loops, best of 3: 231 Âµs per loop
</code></pre>

<p><strong>Further performance boost</strong></p>

<p><code>in1d_approach</code> reduces each row by considering each row as an indexing tuple. We can do the same a bit more efficiently by introducing matrix-multiplication with <code>np.dot</code>, like so -</p>

<pre><code>def in1d_dot_approach(A,B):
    cumdims = (np.maximum(A.max(),B.max())+1)**np.arange(B.shape[1])
    return A[~np.in1d(A.dot(cumdims),B.dot(cumdims))]
</code></pre>

<p>Let's test it against the previous on much larger arrays -</p>

<pre><code>In [251]: # Setup arrays with B as large array and A contains some of B's rows
     ...: B = np.random.randint(0,9,(10000,3))
     ...: A = np.random.randint(0,9,(1000,3))
     ...: A_idx = np.random.choice(np.arange(A.shape[0]),size=10,replace=0)
     ...: B_idx = np.random.choice(np.arange(B.shape[0]),size=10,replace=0)
     ...: A[A_idx] = B[B_idx]
     ...: 

In [252]: %timeit in1d_approach(A,B)
1000 loops, best of 3: 1.28 ms per loop

In [253]: %timeit in1d_dot_approach(A, B)
1000 loops, best of 3: 1.2 ms per loop
</code></pre>

python arrays numpy