pythonnumpyeuclidean-distance

How can the Euclidean distance be calculated with NumPy?


I have two points in 3D space:

a = (ax, ay, az)
b = (bx, by, bz)

I want to calculate the distance between them:

dist = sqrt((ax-bx)^2 + (ay-by)^2 + (az-bz)^2)

How do I do this with NumPy? I have:

import numpy
a = numpy.array((ax, ay, az))
b = numpy.array((bx, by, bz))

Solution

  • Use numpy.linalg.norm:

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

    This works because the Euclidean distance is the l2 norm, and the default value of the ord parameter in numpy.linalg.norm is 2. For more theory, see Introduction to Data Mining:

    enter image description here