0

I have a Numpy array A:

A = np.array([1,2,3,4,5,6,8,10,12,15,20,100,200,300,500])

And another Numpy array B with pairs of numbers:

B = np.array([[2000,1000],[5000,10000],[1,1000],[300,700],[500,5],[500,700],[1,5])

I am looking for the most efficient way to find the index of the first occurrence of the pairs from B that are present in A (if available). Order of numbers within pair doesn't matter. In the example above, the numbers of pair [500,5] represent the first pair that is found in A.

Is there any elegant Numpy solution, without looping through each pair one by one? Appreciate any hints!

loki
  • 976
  • 1
  • 10
  • 22
Franc Weser
  • 767
  • 4
  • 16
  • Have you tried anything, done any research? – AMC Feb 05 '20 at 04:20
  • Does this answer your question? [Python/NumPy first occurrence of subarray](https://stackoverflow.com/questions/7100242/python-numpy-first-occurrence-of-subarray) – AMC Feb 05 '20 at 17:54

3 Answers3

2

I would use isin with argmax

np.isin(B,A).all(1).argmax()

Out[931]: 4

B[np.isin(B,A).all(1).argmax()]

Out[932]: array([500,   5])
Andy L.
  • 24,909
  • 4
  • 17
  • 29
1

You can get the indexes by using np.isin and then considering the minimum element. The first element of the result is the index that satisfies the condition.

>>> np.where(np.isin(B,A).min(axis=1)==1)
(array([4, 6]),)
abc
  • 11,579
  • 2
  • 26
  • 51
  • From the docs for [`numpy.where()`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html): _When only condition is provided, this function is a shorthand for np.asarray(condition).nonzero(). Using nonzero directly should be preferred, as it behaves correctly for subclasses._ – AMC Feb 05 '20 at 04:15
0

How about something like this? This may not be efficient if you really do only care about the first true occurence

match_idxs = np.where(np.isin(B[:,0], A) & np.where(np.isin(B[:,1], A))
idx = matches[0] if len(matches) else None
modesitt
  • 7,052
  • 2
  • 34
  • 64
  • From the docs for [`numpy.where()`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html): _When only condition is provided, this function is a shorthand for np.asarray(condition).nonzero(). Using nonzero directly should be preferred, as it behaves correctly for subclasses._ – AMC Feb 05 '20 at 04:16