june210
june210

Reputation: 35

Difrerent rows in two numpy arrays

I want to filter out elements of a 2d list from another 2d list

c = (array([1, 1, 1, 1]), array([2, 3, 4, 5]))
b = [[1 1 1 1]
 [2 3 4 5]
 [2 3 4 1]
 [4 5 6 7]]

a = itertools.filterfalse(lambda x: x in c, b)

I expect it to give me [[2 3 4 1],[4 5 6 7]] thus filtering out the elements of c from b and returning what remains.

Error: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Upvotes: 1

Views: 96

Answers (2)

yatu
yatu

Reputation: 88236

In order to check membership with 2d arrays, one approach is to view them as 1d arrays. So proceeding similarly to this answer, we could take 1d views of both arrays and use np.setdiff1d to compute the 1d difference:

def view_as1d(a):
    return a.view(np.dtype((np.void, a.dtype.itemsize * a.shape[-1])))

a = np.array(c)
np.setdiff1d(view_as1d(b), view_as1d(a)).view(a.dtype).reshape(-1, a.shape[1])

array([[2, 3, 4, 1],
       [4, 5, 6, 7]])

Upvotes: 1

SUDHEER TALLURI
SUDHEER TALLURI

Reputation: 168

import itertools

c = ([1, 1, 1, 1], [2, 3, 4, 5])

b = [[1,1,1,1],[2,3,4,5],[2,3,4,1],[4,5,6,7]]

a = itertools.filterfalse(lambda x: x in c, b)

This will give the result you require, here it will check the equality between lists and returns boolean values(True/False) and it will give us the result without any error. So, if you have array just convert it to lists and perform the same.

In the case of numpy arrays it doesn't return True or False but returns a boolean numpy array and bool(numpy_array) will lead to error and that's the reason for the error you are getting. But if you have arrays then it won't return boolean values and if you don't want to change arrays to lists then you need to create the boolean values because of which you go for any and all approach as shown.

import itertools
c = (np.array([1, 1, 1, 1]), np.array([2, 3, 4, 5]))
b = [[1,1,1,1],[2,3,4,5],[2,3,4,1],[4,5,6,7]]

a = itertools.filterfalse(lambda x:any((x == arr).all() for arr in c), b)

Thank you!

Upvotes: 0

Related Questions