-5

I was wondering what is the best way to check a row wise contain in python numpy?

Suppose we have a vector V = [1, 2, 3, 4] and a Matrix M = [[2, 3, 4], [3, 5, 6], [4, 1, 3], [5, 4, 2]] (The number of rows in M is equal to the length of V). After performing row wise contain, I should get (False, False, True, True) because 1 is not in [2, 3, 4] and 2 is not in [3, 5, 6] and 3 is in [4, 1, 3] and 4 is in [5, 4, 2]

What would be the best way to do this operation in python numpy?

Actually, I do not want to use a for loop. That obviously could work while is not the best way to do it. I myself came up with this idea to do a subtraction and then count the number of zeros in the result which is much faster than using for loops. However, I wanted to know if there is a better way to do it.

Siavash
  • 503
  • 1
  • 5
  • 25

1 Answers1

6

What you're looking for is the in operator. e.g. 1 in [1,2,3] returns True So given your values of v and m as numpy arrays as follows:

import numpy as np
v = np.array([1,2,3,4])
m = np.array([np.array([2,3,4]), np.array([3,5,6]), np.array([4,1,3]), np.array([5,4,2])])    
# Checking row wise contain
result = [(v[i] in m[i]) for i in range(len(v))]

The result is:

>>> [(v[i] in m[i]) for i in range(len(v))]
[False, False, True, True]

Another solution as Divakar pointed out would be to use

>>> (m==v[:,None]).any(1)
array([False, False,  True,  True], dtype=bool)

However, doing some rough timing checks:

>>> start_time=time.time(); (m==v[:,None]).any(1); print(time.time()-start_time)
array([False, False,  True,  True], dtype=bool)
0.000586032867432
>>> start_time=time.time(); [(v[i] in m[i]) for i in range(len(v))]; print(time.time()-start_time)
[False, False, True, True]
7.20024108887e-05

The initial solution seems to be faster.

Sudheesh Singanamalla
  • 2,283
  • 3
  • 19
  • 36