How to check if one numpy array a contains fully another numpy array b efficiently? Somewhat like b is subset of a....
Thanks!
EDIT: a and b are one dimentional numpy arrays
How to check if one numpy array a contains fully another numpy array b efficiently? Somewhat like b is subset of a....
Thanks!
EDIT: a and b are one dimentional numpy arrays
If you're asking about b being a consecutive sub-array of a
If either of the arrays can contain repeated values, algorithmically speaking, your problem is equivalent to a single-pattern string-searching problem. There are several known algorithms for this problem. Unfortunately, neither is too simple.
Else, it is simple to implement, by first looking for the first element in b, then comparing all the following elements:
import numpy as np
def is_subarray_no_repeatition(a, b):
try:
i = np.where(a == b[0])[0][0]
except IndexError:
# either b is empty, or b[0] not in a
return b.size == 0
a = a[i : i+b.size]
if a.size < b.size:
return False
return (a == b).all()
If you're asking about b being a subset of a (i.e. that each element of b exists in a)
def is_subset(a, b):
b = np.unique1d(b)
c = np.intersect1d(a,b)
return c.size == b.size