Home > Enterprise >  Python Extracting rows not in another numpy array
Python Extracting rows not in another numpy array

Time:12-01

Given two numpy matrices 'a' and 'b', I am trying to extract rows in 'a' that are not in 'b'. The problem is the dimension of 'b' is not fixed. If I use .tolist(), then it does not work when 'b' has dimension = 1, since it considers each row with individual elements of 'b' instead of the entire 'b' array.

Here are results of some functions that I tried: Link to the image

In the image, the first and last result is correct. If 'b' is a matrix, then converting to list works, but if it is an array, then .all().any() works.

np.isin() also does not work since 'a' is multi-dimensional.

What is a general way to achieve this?

CodePudding user response:

np.isin actually works and the correct way to do is like this:

>>> a = np.arange(1, 10).reshape(3,3)
>>> a
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> b
array([[1, 2, 3],
       [4, 5, 6]])
>>> c = np.arange(1, 4).reshape(1, 3)
>>> c
array([[1, 2, 3]])

Then elements in a not in b are:

>>> a[~np.isin(a,b)].reshape(-1, a.shape[1])
array([[7, 8, 9]])

Elements in a not in c are (assuming the no. of columns are same in both matrices):

>>> a[~np.isin(a,c)].reshape(-1, a.shape[1])
array([[4, 5, 6],
       [7, 8, 9]])
  • Related