AlgoDaily

What is the Euclidean Distance?

Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.

Introduction

What is the Euclidean Distance?

In short the Euclidean Distance represents the shortest distance between two points. You are most likely to use this method when calculating the distance between two rows of data that have numerical values, such a floating point or integer values.

Imagine like the image below, you are taking a trip from Barcelona to Berlin. Of course the fastest method of transport is to fly, but how far exactly is this journey?

Pythagorean Theorem

Pythagorean Theorem

Enter Pythagoras, a Greek philosopher and inventor of the infamous Pythagorean Theorem which stated that:

"In a right-angled triangle, the sum of the square of the hypotenuse side is equal to the sum of the squares other two sides."

So using this theorem:

The distance from Barcelona to Berlin squared (AC²) is equal to AB² + BC².

Therefore AC is equal √ (AB² + BC²).

Since AB is equal to (x2 - x1) and likewise BC is equal to (y2 - y1), we adjust our formula and we get this, the simplest formula for Euclidean Distance:

= √[(x2-x1)² + (y2-y1)²]

# Import libraries
from scipy.spatial import distance

#2D
p_1 = (6,5)
p_2 = (3,2)

euclidean_distance = distance.euclidean(p_1, p_2)
print('Euclidean Distance between', p_1, 'and', p_2, 'is', euclidean_distance)

Output: Euclidean Distance between (6, 5) and (3, 2) is 4.242640687119285

Multiple Choice

Calculate the Euclidean distance between the points P(3, 2) and Q(4,1)

Adding Dimensions

Adding Dimensions

Suppose we have 3 Dimensions, the points now become (x1, y1, z1) and (x2, y, z2), our formula simply changes to:

= √[(x2-x1)² + (y2-y1)² + (z2-z1)²]

#3D
p_1 = (6,5,4)
p_2 = (3,2,1)

euclidean_distance = distance.euclidean(p_1, p_2)
print('Euclidean Distance between', p_1, 'and', p_2, 'is', euclidean_distance)

Output: Euclidean Distance between (6, 5, 4) and (3, 2, 1) is 5.196152422706632

As you can see we just add it to our formula, easy right?!

Multiple Choice

Calculate the Euclidean distance between the points P(3,6,1) and Q(4,1,5).

Conclusion

Conclusion

Euclidean Distance is one of the most common distance measures used in ML. Distance measures play an important role in Machine Learning. It provides the foundation for many popular and effective ML algorithms like k-nearest neighbors for supervised learning and k-means clustering for unsupervised learning.

One Pager Cheat Sheet

One Pager Cheat Sheet

  • The Euclidean Distance is the shortest distance between two points, typically used to calculate the distance between two rows of numeric values such as floating point or integer values.
  • The Pythagorean Theorem can be used to compute the Euclidean Distance between two points, or two sets of coordinates, by calculating the square root of the sum of their squared differences.
  • The Euclidean distance between two points in a two-dimensional space can be calculated using the Pythagorean Theorem formula √[(x2-x1)² + (y2-y1)²].
  • The formula to find the Euclidean distance between two points of 3 Dimensions is merely the addition of their respective coordinates, calculated by the euclidean_distance function.
  • The Euclidean distance between the points P(3,6,1) and Q(4,1,5) is calculated using the formula √[(x2-x1)² + (y2-y1)² + (z2-z1)²], which results in a distance of 6.5.
  • Euclidean Distance is a widely used distance measure in Machine Learning, which is essential for many popular algorithms like k-nearest neighbors and k-means clustering.
Open interactive view →