How can the Euclidean distance be calculated with NumPy?


Question

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))
1
447
6/26/2018 8:26:28 PM

Accepted Answer

Use numpy.linalg.norm:

dist = numpy.linalg.norm(a-b)
745
7/27/2013 7:40:17 PM

There's a function for that in SciPy. It's called Euclidean.

Example:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon