[英]In Numpy, find Euclidean distance between each pair from two arrays
I have two arrays of 2D coordinate points (x,y) 我有两个2D坐标点数组(x,y)
a = [ (x1,y1), (x2,y2), ... (xN,yN) ]
b = [ (X1,Y1), (X2,Y2), ... (XN,YN) ]
How can I find the Euclidean distances between each aligned pairs (xi,yi) to (Xi,Yi)
in an 1xN
array? 如何在
1xN
阵列中找到每个对齐对(xi,yi) to (Xi,Yi)
之间的欧几里德距离?
The scipy.spatial.cdist
function gives me distances between all pairs in an NxN
array. scipy.spatial.cdist
函数给出了NxN
数组中所有对之间的距离。
If I just use norm
function to calculate the distance one by one it seems to be slow. 如果我只是使用
norm
函数逐个计算距离,它似乎很慢。
Is there a built in function to do this? 是否有内置功能来执行此操作?
I'm not seeing a built-in, but you could do it yourself pretty easily. 我没有看到内置的,但你可以很容易地自己做。
distances = (a-b)**2
distances = distances.sum(axis=-1)
distances = np.sqrt(distances)
hypot
is another valid alternative hypot
是另一种有效的替代品
a, b = randn(10, 2), randn(10, 2)
ahat, bhat = (a - b).T
r = hypot(ahat, bhat)
Result of timeit
s between manual calculation and hypot
: 的结果
timeit
手工计算和S之间hypot
:
Manual: 手册:
timeit sqrt(((a - b) ** 2).sum(-1))
100000 loops, best of 3: 10.3 µs per loop
Using hypot
: 使用
hypot
:
timeit hypot(ahat, bhat)
1000000 loops, best of 3: 1.3 µs per loop
Now how about some adult-sized arrays: 现在一些成人大小的阵列怎么样:
a, b = randn(1e7, 2), randn(1e7, 2)
ahat, bhat = (a - b).T
timeit -r10 -n3 hypot(ahat, bhat)
3 loops, best of 10: 208 ms per loop
timeit -r10 -n3 sqrt(((a - b) ** 2).sum(-1))
3 loops, best of 10: 224 ms per loop
Not much of a performance difference between the two methods. 这两种方法之间没有太大的性能差异。 You can squeeze out a tiny bit more from the latter by avoiding
pow
: 你可以通过避免使用
pow
来挤出更多的东西:
d = a - b
timeit -r10 -n3 sqrt((d * d).sum(-1))
3 loops, best of 10: 184 ms per loop
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.