Nathan Fellman Nathan Fellman - 2 years ago 206
Python Question

How can the euclidean distance be calculated with numpy?

I have two points in 3D:

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

And I want to calculate the distance:

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

What's the best way to do this with Numpy, or with Python in general? I have:

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

Answer Source

Use numpy.linalg.norm:

dist = numpy.linalg.norm(a-b)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download