3

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

5
  • @jterrace yes a and b are one dimentional Commented Apr 8, 2014 at 19:59
  • can either a or b contain repeated values? Commented Apr 8, 2014 at 20:07
  • maybe this post Commented Apr 8, 2014 at 20:19
  • @neel101 Do you need to check if each element of b exists in a, or if b exists in a as a (consecutive) sub-array? Commented Apr 8, 2014 at 20:46
  • just only want to check if each element of b exist in a... and your answer worked thanks! :) Commented Apr 8, 2014 at 20:50

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Isn't there any function in numpy that can do this job? I was using np.intersect1d(a, b) == b: which unfortunately was giving shape mismatch error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.