NumPy: Calculate the Euclidean distance

NumPy: Array Object Exercise-103 with Solution

Write a NumPy program to calculate the Euclidean distance.

From Wikipedia: In mathematics, the Euclidean distance or Euclidean metric is the "ordinary" straight-line distance between two points in Euclidean space. With this distance, Euclidean space becomes a metric space. The associated norm is called the Euclidean norm. Older literature refers to the metric as the Pythagorean metric

Sample Solution:

Python Code:

from scipy.spatial import distance
p1 = (1, 2, 3)
p2 = (4, 5, 6)
d = distance.euclidean(p1, p2)
print("Euclidean distance: ",d)

Sample Output:

Euclidean distance:  5.196152422706632

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to convert a NumPy array into a csv file.
Next: Write a NumPy program to access last two columns of a multidimensional columns.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

Python: Tips of the Day

List comprehension:

>>> m = [x ** 2 for x in range(5)]
>>> m
[0, 1, 4, 9, 16]