![](/img/trans.png)
[英]Fastest way to find all indexes of matching values between two 1D arrays (with duplicates)
[英]Fastest way to find the nearest pairs between two numpy arrays without duplicates
给定两个大的 numpy arrays A
和B
具有不同的行数( len(B) > len(A)
)但相同的列数( A.shape[1] = B.shape[1] = 3
)。 我想知道从B
获得子集C
的最快方法,该子集具有最小总距离(所有成对距离的总和)到A
而没有重复(每对必须都是唯一的)。 这意味着C
应该具有与A
相同的形状。
下面是我的代码,但有两个主要问题:
np.linalg.norm
(需要注意周期性边界条件)。 我认为这绝对不是 go 的最快方法,因为下面的代码每次调用一对距离计算 function。 当我调用更昂贵的距离计算 function 并且它将永远运行时,会有很大的开销。 有什么建议么?import numpy as np
from operator import itemgetter
import random
import time
A = 100.*np.random.rand(1000, 3)
B = A.copy()
for (i,j), _ in np.ndenumerate(B):
B[i,j] += np.random.rand()
B = np.vstack([B, 100.*np.random.rand(500, 3)])
def calc_dist(x, y):
return np.linalg.norm(x - y)
t0 = time.time()
taken = []
for rowi in A:
res = min(((k, calc_dist(rowi, rowj)) for k, rowj in enumerate(B)
if k not in taken), key=itemgetter(1))
taken.append(res[0])
C = B[taken]
print(A.shape, B.shape, C.shape)
>>> (1000, 3) (1500, 3) (1000, 3)
print(time.time() - t0)
>>> 12.406389951705933
编辑:对于那些对昂贵的距离计算 function 感兴趣的人,它使用ase
package (可以通过pip install ase
)
from ase.geometry import find_mic
def calc_mic_dist(x, y):
return find_mic(np.array([x]) - np.array([y]),
cell=np.array([[50., 0.0, 0.0],
[25., 45., 0.0],
[0.0, 0.0, 100.]]))[1][0]
如果您可以计算整个 N² 距离,这对于您给出的尺寸来说并不昂贵, scipy.optimize
有一个 function 可以直接解决这个问题。
import scipy.optimize
cost = np.linalg.norm(A[:, np.newaxis, :] - B, axis=2)
_, indexes = scipy.optimize.linear_sum_assignment(cost)
C = B[indexes]
利用 numpy 广播和矢量化的强大功能
find_mic
中的ase.geometry
方法可以处理 2d np arrays。
from ase.geometry import find_mic
def calc_mic_dist(x, y):
return find_mic(x - y,
cell=np.array([[50., 0.0, 0.0],
[25., 45., 0.0],
[0.0, 0.0, 100.]]))[1]
测试:
x = np.random.randn(1,3)
y = np.random.randn(5,3)
print (calc_mic_dist(x,y).shape)
# It is a distance metrics so:
assert np.allclose(calc_mic_dist(x,y), calc_mic_dist(y,x))
输出:
(5,)
如您所见,指标是针对x
的每个值和y
的每个值计算的,因为 numpy 中的xy
具有广播的魔力。
def calc_mic_dist(x, y):
return find_mic(x - y,
cell=np.array([[50., 0.0, 0.0],
[25., 45., 0.0],
[0.0, 0.0, 100.]]))[1]
t0 = time.time()
A = 100.*np.random.rand(1000, 3)
B = 100.*np.random.rand(5000, 3)
selected = [np.argmin(calc_mic_dist(a, B)) for a in A]
C = B[selected]
print (A.shape, B.shape, C.shape)
print (f"Time: {time.time()-t0}")
Output:
(1000, 3) (5000, 3) (1000, 3)
Time: 9.817562341690063
谷歌合作大约需要 10 秒
我们知道calc_mic_dist(x,x)
== 0
所以如果A
是B
的子集,那么C
应该正好是A
A = 100.*np.random.rand(1000, 3)
B = np.vstack([100.*np.random.rand(500, 3), A, 100.*np.random.rand(500, 3)])
selected = [np.argmin(calc_mic_dist(a, B)) for a in A]
C = B[selected]
print (A.shape, B.shape, C.shape)
print (np.allclose(A,C))
Output:
(1000, 3) (2000, 3) (1000, 3)
True
一旦选择了
B
中的向量,就不能再次为A
的其他值选择它
这可以通过从B
中删除选定的向量来实现,一旦它被选中,它就不会再次出现在A
的下一行作为可能的候选者。
A = 100.*np.random.rand(1000, 3)
B = np.vstack([100.*np.random.rand(500, 3), A, 100.*np.random.rand(500, 3)])
B_ = B.copy()
C = np.zeros_like(A)
for i, a in enumerate(A):
s = np.argmin(calc_mic_dist(a, B_))
C[i] = B_[s]
# Remove the paried
B_ = np.delete(B_, (s), axis=0)
print (A.shape, B.shape, C.shape)
print (np.allclose(A,C))
Output:
(1000, 3) (2000, 3) (1000, 3)
True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.