Finding the Euclidean Distance in 3D Space Using NumPy
When dealing with three-dimensional points, like:
``
a = (ax, ay, az)
b = (bx, by, bz)
``
Determining the distance between them becomes essential. The Euclidean distance, given by the formula:
``
dist = sqrt((ax-bx)^2 (ay-by)^2 (az-bz)^2)
``
can be effortlessly calculated using NumPy.
To accomplish this, harness the power of numpy.linalg.norm, as demonstrated below:
``
import numpy
a = numpy.array((ax, ay, az))
b = numpy.array((bx, by, bz))
dist = numpy.linalg.norm(a-b)
``
The caveat here lies in numpy.linalg.norm's default ordinal parameter (ord) being set to 2. This effectively aligns with the calculation of the Euclidean distance, which is inherently an l2 norm.
For further insights into the theoretical underpinnings of Euclidean distance and norm, delve into the classic work "Introduction to Data Mining".
The above is the detailed content of How Can NumPy Efficiently Calculate Euclidean Distance in 3D Space?. For more information, please follow other related articles on the PHP Chinese website!