欧几里德距离如何用numpy来计算?

我在3D中有两点:

(xa, ya, za)
(xb, yb, zb)

我想计算距离:

dist = sqrt((xa-xb)^2 + (ya-yb)^2 + (za-zb)^2)

用Numpy或Python来做这件事最好的办法是什么? 我有:

a = numpy.array((xa ,ya, za))
b = numpy.array((xb, yb, zb))

使用numpy.linalg.norm

dist = numpy.linalg.norm(a-b)

SciPy中有一个功能,叫做欧几里得

例:

from scipy.spatial import distance
a = (1,2,3)
b = (4,5,6)
dst = distance.euclidean(a,b)

这个问题解决方法的另一个例子。 只要我提交了问题,我就明白了:

def dist(x,y):   
    return numpy.sqrt(numpy.sum((x-y)**2))

a = numpy.array((xa,ya,za))
b = numpy.array((xb,yb,zb))
dist_a_b = dist(a,b)
链接地址: http://www.djcxy.com/p/50979.html

上一篇: How can the euclidean distance be calculated with numpy?

下一篇: Allocating a user defined struct in shared memory with boost::interprocess