user2539822
user2539822

Reputation:

How to check efficiently if numpy array a contains b

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

Upvotes: 3

Views: 1826

Answers (1)

shx2
shx2

Reputation: 64368

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

Upvotes: 1

Related Questions