Home > Backend Development > Python Tutorial > How Can NumPy Efficiently Calculate Euclidean Distance in 3D Space?

How Can NumPy Efficiently Calculate Euclidean Distance in 3D Space?

Susan Sarandon
Release: 2024-12-10 10:35:10
Original
970 people have browsed it

How Can NumPy Efficiently Calculate Euclidean Distance in 3D Space?

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template