9

How can I do the "in" operation on a numpy array? (Return True if an element is present in the given numpy array)

For strings, lists and dictionaries, the functionality is intuitive to understand.

Here's what I got when I applied that on a numpy array

a
array([[[2, 3, 0],
    [1, 0, 1]],

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

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

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

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

b = [[3, 2, 0],
    [0, 1, 1]]

b in a
True
#Aligned with the expectation

c = [[300, 200, 0],
    [0, 100, 100]]

c in a
True
#Not quite what I expected
1
  • For 1d arrays there is a np.in1d, but applying it to rows of a 2d array requires some tricks. Look at it's code to see what is involved. Commented Sep 12, 2016 at 16:13

3 Answers 3

7

You could compare the input arrays for equality, which will perform broadcasted comparisons across all elements in a at each position in the last two axes against elements at corresponding positions in the second array. This will result in a boolean array of matches, in which we check for ALL matches across the last two axes and finally check for ANY match, like so -

((a==b).all(axis=(1,2))).any()

Sample run

1) Inputs :

In [68]: a
Out[68]: 
array([[[2, 3, 0],
        [1, 0, 1]],

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

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

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

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

In [69]: b
Out[69]: 
array([[3, 2, 0],
       [0, 1, 1]])

2) Broadcasted elementwise comparisons :

In [70]: a==b
Out[70]: 
array([[[False, False,  True],
        [False, False,  True]],

       [[ True,  True,  True],
        [ True,  True,  True]],

       [[False,  True,  True],
        [False,  True,  True]],

       [[False, False,  True],
        [False, False,  True]],

       [[ True, False,  True],
        [ True, False,  True]]], dtype=bool)

3) ALL match across last two axes and finally ANY match :

In [71]: (a==b).all(axis=(1,2))
Out[71]: array([False,  True, False, False, False], dtype=bool)

In [72]: ((a==b).all(axis=(1,2))).any()
Out[72]: True

Following similar steps for c in a -

In [73]: c
Out[73]: 
array([[300, 200,   0],
       [  0, 100, 100]])

In [74]: ((a==c).all(axis=(1,2))).any()
Out[74]: False
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for saving the day! So no straightforward way like "in" to check the existence on a numpy array?
@akilat90 I am afraid there isn't. Or just wrap it into a function and there you go, a custom made straightforward way!
3

Since I do not have enough reputation to comment but the answer suggested by @rp0 using np.isin and then np.all will not work with numpy arrays. Counter Example:

a = np.array([[[2, 3, 0],
               [1, 0, 1]],
              [[3, 2, 0],
               [0, 1, 1]],
              [[2, 2, 0],
               [1, 1, 1]],
              [[1, 3, 0],
               [2, 0, 1]],
              [[3, 1, 0],
               [0, 2, 1]]])
b = [[3, 2, 0],
     [0, 1, 1]]

c = [[3, 3, 3],
     [3, 3, 3]]

print(np.isin(b,a))
[[ True  True  True]
 [ True  True  True]]

print(np.isin(c,a))
[[ True  True  True]
 [ True  True  True]]

For completeness, I converted the array to python list using tolist and then used normal 'in' operator and its worked just as expected.

print(b in a.lolist())
True

print(c in a.tolist())
False

Comments

1

This question is fairly old, but if you're like me, you might have thought there was no numpy equivalent for in by reading it.

Numpy 1.13 was released in 2017, and with it came the function isin(), which now nicely solves the problem:

import numpy as np

a = np.array([[[2, 3, 0],
               [1, 0, 1]],
              [[3, 2, 0],
               [0, 1, 1]],
              [[2, 2, 0],
               [1, 1, 1]],
              [[1, 3, 0],
               [2, 0, 1]],
              [[3, 1, 0],
               [0, 2, 1]]])

b = [[3, 2, 0],
     [0, 1, 1]]

print np.isin(b,a)

# [[ True  True  True]
#  [ True  True  True]]

c = [[300, 200, 0],
    [0, 100, 100]]

print np.isin(c,a)

# [[False False  True]
#  [ True False False]]

You'll probably want to use np.all() at the end if you're looking for the entire element to be in the test array.

1 Comment

@AturSams This is not probably what is expected. The matches are regardles the item position and np.all() with np.isin() will not do what you probably expect as soo-zie pointed out

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.