在两个数组中查找公共值的索引

时间:2022-07-19 12:13:12

I'm using Python 2.7. I have two arrays, A and B. To find the indices of the elements in A that are present in B, I can do

我正在使用Python 2.7。我有两个数组,A和B.为了找到B中存在的元素的索引,我可以做到

A_inds = np.in1d(A,B)

I also want to get the indices of the elements in B that are present in A, i.e. the indices in B of the same overlapping elements I found using the above code.

我还想得到A中存在的B中元素的索引,即使用上面的代码找到的相同重叠元素的B中的索引。

Currently I am running the same line again as follows:

目前我再次运行同一行,如下所示:

B_inds = np.in1d(B,A)

but this extra calculation seems like it should be unnecessary. Is there a more computationally efficient way of obtaining both A_inds and B_inds?

但这个额外的计算似乎应该是不必要的。是否有更高效的计算方法来获得A_inds和B_inds?

I am open to using either list or array methods.

我愿意使用列表或数组方法。

2 个解决方案

#1


3  

np.unique and np.searchsorted could be used together to solve it -

np.unique和np.searchsorted可以一起使用来解决它 -

def unq_searchsorted(A,B):

    # Get unique elements of A and B and the indices based on the uniqueness
    unqA,idx1 = np.unique(A,return_inverse=True)
    unqB,idx2 = np.unique(B,return_inverse=True)

    # Create mask equivalent to np.in1d(A,B) and np.in1d(B,A) for unique elements
    mask1 = (np.searchsorted(unqB,unqA,'right') - np.searchsorted(unqB,unqA,'left'))==1
    mask2 = (np.searchsorted(unqA,unqB,'right') - np.searchsorted(unqA,unqB,'left'))==1

    # Map back to all non-unique indices to get equivalent of np.in1d(A,B), 
    # np.in1d(B,A) results for non-unique elements
    return mask1[idx1],mask2[idx2]

Runtime tests and verify results -

运行时测试并验证结果 -

In [233]: def org_app(A,B):
     ...:     return np.in1d(A,B), np.in1d(B,A)
     ...: 

In [234]: A = np.random.randint(0,10000,(10000))
     ...: B = np.random.randint(0,10000,(10000))
     ...: 

In [235]: np.allclose(org_app(A,B)[0],unq_searchsorted(A,B)[0])
Out[235]: True

In [236]: np.allclose(org_app(A,B)[1],unq_searchsorted(A,B)[1])
Out[236]: True

In [237]: %timeit org_app(A,B)
100 loops, best of 3: 7.69 ms per loop

In [238]: %timeit unq_searchsorted(A,B)
100 loops, best of 3: 5.56 ms per loop

If the two input arrays are already sorted and unique, the performance boost would be substantial. Thus, the solution function would simplify to -

如果两个输入数组已经排序且唯一,那么性能提升将是巨大的。因此,解决方案功能将简化为 -

def unq_searchsorted_v1(A,B):
    out1 = (np.searchsorted(B,A,'right') - np.searchsorted(B,A,'left'))==1
    out2 = (np.searchsorted(A,B,'right') - np.searchsorted(A,B,'left'))==1  
    return out1,out2

Subsequent runtime tests -

后续运行时测试 -

In [275]: A = np.random.randint(0,100000,(20000))
     ...: B = np.random.randint(0,100000,(20000))
     ...: A = np.unique(A)
     ...: B = np.unique(B)
     ...: 

In [276]: np.allclose(org_app(A,B)[0],unq_searchsorted_v1(A,B)[0])
Out[276]: True

In [277]: np.allclose(org_app(A,B)[1],unq_searchsorted_v1(A,B)[1])
Out[277]: True

In [278]: %timeit org_app(A,B)
100 loops, best of 3: 8.83 ms per loop

In [279]: %timeit unq_searchsorted_v1(A,B)
100 loops, best of 3: 4.94 ms per loop

#2


1  

A simple multiprocessing implementation will get you a little more speed:

一个简单的多处理实现将使您获得更快的速度:

import time
import numpy as np

from multiprocessing import Process, Queue

a = np.random.randint(0, 20, 1000000)
b = np.random.randint(0, 20, 1000000)

def original(a, b, q):
    q.put( np.in1d(a, b) )

if __name__ == '__main__':
    t0 = time.time()
    q = Queue()
    q2 = Queue()
    p = Process(target=original, args=(a, b, q,))
    p2 = Process(target=original, args=(b, a, q2))
    p.start()
    p2.start()
    res = q.get()
    res2 = q2.get()

    print time.time() - t0

>>> 0.21398806572 

Divakar's unq_searchsorted(A,B) method took 0.271834135056 seconds on my machine.

Divakar的unq_searchsorted(A,B)方法在我的机器上花了0.271834135056秒。

#1


3  

np.unique and np.searchsorted could be used together to solve it -

np.unique和np.searchsorted可以一起使用来解决它 -

def unq_searchsorted(A,B):

    # Get unique elements of A and B and the indices based on the uniqueness
    unqA,idx1 = np.unique(A,return_inverse=True)
    unqB,idx2 = np.unique(B,return_inverse=True)

    # Create mask equivalent to np.in1d(A,B) and np.in1d(B,A) for unique elements
    mask1 = (np.searchsorted(unqB,unqA,'right') - np.searchsorted(unqB,unqA,'left'))==1
    mask2 = (np.searchsorted(unqA,unqB,'right') - np.searchsorted(unqA,unqB,'left'))==1

    # Map back to all non-unique indices to get equivalent of np.in1d(A,B), 
    # np.in1d(B,A) results for non-unique elements
    return mask1[idx1],mask2[idx2]

Runtime tests and verify results -

运行时测试并验证结果 -

In [233]: def org_app(A,B):
     ...:     return np.in1d(A,B), np.in1d(B,A)
     ...: 

In [234]: A = np.random.randint(0,10000,(10000))
     ...: B = np.random.randint(0,10000,(10000))
     ...: 

In [235]: np.allclose(org_app(A,B)[0],unq_searchsorted(A,B)[0])
Out[235]: True

In [236]: np.allclose(org_app(A,B)[1],unq_searchsorted(A,B)[1])
Out[236]: True

In [237]: %timeit org_app(A,B)
100 loops, best of 3: 7.69 ms per loop

In [238]: %timeit unq_searchsorted(A,B)
100 loops, best of 3: 5.56 ms per loop

If the two input arrays are already sorted and unique, the performance boost would be substantial. Thus, the solution function would simplify to -

如果两个输入数组已经排序且唯一,那么性能提升将是巨大的。因此,解决方案功能将简化为 -

def unq_searchsorted_v1(A,B):
    out1 = (np.searchsorted(B,A,'right') - np.searchsorted(B,A,'left'))==1
    out2 = (np.searchsorted(A,B,'right') - np.searchsorted(A,B,'left'))==1  
    return out1,out2

Subsequent runtime tests -

后续运行时测试 -

In [275]: A = np.random.randint(0,100000,(20000))
     ...: B = np.random.randint(0,100000,(20000))
     ...: A = np.unique(A)
     ...: B = np.unique(B)
     ...: 

In [276]: np.allclose(org_app(A,B)[0],unq_searchsorted_v1(A,B)[0])
Out[276]: True

In [277]: np.allclose(org_app(A,B)[1],unq_searchsorted_v1(A,B)[1])
Out[277]: True

In [278]: %timeit org_app(A,B)
100 loops, best of 3: 8.83 ms per loop

In [279]: %timeit unq_searchsorted_v1(A,B)
100 loops, best of 3: 4.94 ms per loop

#2


1  

A simple multiprocessing implementation will get you a little more speed:

一个简单的多处理实现将使您获得更快的速度:

import time
import numpy as np

from multiprocessing import Process, Queue

a = np.random.randint(0, 20, 1000000)
b = np.random.randint(0, 20, 1000000)

def original(a, b, q):
    q.put( np.in1d(a, b) )

if __name__ == '__main__':
    t0 = time.time()
    q = Queue()
    q2 = Queue()
    p = Process(target=original, args=(a, b, q,))
    p2 = Process(target=original, args=(b, a, q2))
    p.start()
    p2.start()
    res = q.get()
    res2 = q2.get()

    print time.time() - t0

>>> 0.21398806572 

Divakar's unq_searchsorted(A,B) method took 0.271834135056 seconds on my machine.

Divakar的unq_searchsorted(A,B)方法在我的机器上花了0.271834135056秒。