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
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)²]
1# Import libraries
2from scipy.spatial import distance
3
4#2D
5p_1 = (6,5)
6p_2 = (3,2)
7
8euclidean_distance = distance.euclidean(p_1, p_2)
9print('Euclidean Distance between', p_1, 'and', p_2, 'is', euclidean_distance)
Output: Euclidean Distance between (6, 5) and (3, 2) is 4.242640687119285
Build your intuition. Click the correct answer from the options.
Calculate the Euclidean distance between the points P(3, 2) and Q(4,1)
Click the option that best answers the question.
- 1.4
- 4.5
- 2.3
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)²]
1#3D
2p_1 = (6,5,4)
3p_2 = (3,2,1)
4
5euclidean_distance = distance.euclidean(p_1, p_2)
6print('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?!
Build your intuition. Click the correct answer from the options.
Calculate the Euclidean distance between the points P(3,6,1) and Q(4,1,5).
Click the option that best answers the question.
- 6.5
- 2.5
- 3.2
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
- 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
orinteger
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 theeuclidean_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 likek-nearest neighbors
andk-means clustering
.