1

I have two 2d array, A and B, like the following.

A=array([[1, 1, 1],
       [2, 2, 2],
       [3, 3, 3],
       [4, 4, 4],
       [5, 5, 5],
       [6, 6, 6],
       [7, 7, 7],
       [8, 8, 8],
       [9, 9, 9]])
B=array([[1, 1, 1],
       [3, 3, 3],
       [8, 8, 8]])

I want to remove subarrays of A if they exist in B. Then return a new 2d array C like the following:

C=array([[2, 2, 2],
       [4, 4, 4],
       [5, 5, 5],
       [6, 6, 6],
       [7, 7, 7],
       [9, 9, 9]])

Currently I have tried the np.isin function but the result is no longer a 2d array.

mask = np.isin(A, B, invert=True)
A[mask]
>>array([2, 2, 2, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 9, 9, 9])

2 Answers 2

1

You need to aggregate the booleans on axis 1:

C = A[~np.isin(A,B).all(1)]

output:

array([[2, 2, 2],
       [4, 4, 4],
       [5, 5, 5],
       [6, 6, 6],
       [7, 7, 7],
       [9, 9, 9]])
Sign up to request clarification or add additional context in comments.

Comments

0

Or in1d:

C = A[~np.in1d(A, B).all(axis=1)]

And now:

print(C)

Output:

[[2 2 2]
 [4 4 4]
 [5 5 5]
 [6 6 6]
 [7 7 7]
 [9 9 9]]

3 Comments

This is incorrect, that would match only the first column
@mozway Edited mine
numpy.in1d is semi-deprecated. From the docs: We recommend using :func:isin instead of in1d for new code. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.