'Return common element indices between two numpy arrays
I have two arrays, a1 and a2. Assume len(a2) >> len(a1), and that a1 is a subset of a2.
I would like a quick way to return the a2 indices of all elements in a1. The time-intensive way to do this is obviously:
from operator import indexOf
indices = []
for i in a1:
indices.append(indexOf(a2,i))
This of course takes a long time where a2 is large. I could also use numpy.where() instead (although each entry in a1 will appear just once in a2), but I'm not convinced it will be quicker. I could also traverse the large array just once:
for i in xrange(len(a2)):
if a2[i] in a1:
indices.append(i)
But I'm sure there is a faster, more 'numpy' way - I've looked through the numpy method list, but cannot find anything appropriate.
Many thanks in advance,
D
Solution 1:[1]
how about:
wanted = set(a1)
indices =[idx for (idx, value) in enumerate(a2) if value in wanted]
This should be O(len(a1)+len(a2)) instead of O(len(a1)*len(a2))
NB I don't know numpy so there may be a more 'numpythonic' way to do it, but this is how I would do it in pure python.
Solution 2:[2]
index = in1d(a2,a1)
result = a2[index]
Solution 3:[3]
Very similar to @AlokSinghal, but you get an already flattened version.
numpy.flatnonzero(numpy.in1d(a2, a1))
Solution 4:[4]
The numpy_indexed package (disclaimer: I am its author) contains a vectorized equivalent of list.index; performance should be similar to the currently accepted answer, but as a bonus, it gives you explicit control over missing values as well, using the 'missing' kwarg.
import numpy_indexed as npi
indices = npi.indices(a2, a1, missing='raise')
Also, it will also work on multi-dimensional arrays, ie, finding the indices of one set of rows in another.
Solution 5:[5]
These all methods are slow for me. Following method is doing quite fast. The index list has the index of the elements from first list which are common in second list.
index=[]
d={}
for j in range(len(first_list)):
name=first_list[j]
d[name]=j
for i in range(len(second_list)):
name=second_list[i]
index.append(d[name])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | chriad |
| Solution 3 | Philippe Miron |
| Solution 4 | Eelco Hoogendoorn |
| Solution 5 | ankit agrawal |
