How to Calculate Euclidean Distance in Python

The Euclidean distance between two points in either the plane or 3-dimensional space measures the length of a segment connecting the two points. It is the most obvious way of representing the distance between two points.

In this tutorial, we will discuss about how to calculate Euclidean distance in python

Euclidean Distance Formula

The Euclidean distance between two vectors, P and Q, is calculated as:

Euclidean distance = √Σ(Pi-Qi)2

Numpy for Euclidean Distance

We will be using numpy library available in python to calculate the Euclidean distance between two vectors.

If you don’t have numpy library installed then use the below command on the windows command prompt for numpy library installation

pip install numpy

How to Calculate Euclidean Distance in python

In python, the numpy library provides linalg.norm() function to calculate the Euclidean distance.

Let’s understand with examples about how to calculate Euclidean distance in python with given below python code.

Example #1 Euclidean Distance Calculation

#import modules
import numpy as np
from numpy.linalg import norm

#Define Vectors
p = np.array([2, 3,4,2])
q = np.array([1,-2,1,3])

#Calculate Euclidean distance between the two vectors 
result = norm(p-q)

print("The Euclidean distance between the two Vectors: ",result)

In the above example, we have created a p and q array of the same length using numpy package array function.

Python function norm() accepts p and q array as input parameters and returns the Euclidean distance as the result.

The above code gives Euclidean distance between the two Vectors for given p and q array is 6.0. The output of the above code as below.

//Output
The Euclidean distance between the two Vectors:  6.0

Example #2 Euclidean Distance Calculation

Let’s take another example to find Euclidean distance between two arrays of different length with given below python code.

#import modules
import numpy as np
from numpy.linalg import norm

#Define Vectors
p = np.random.randint(10, size=90) #length=90
q = np.random.randint(10, size=100) #length=100

#Calculate Euclidean distance between the two vectors 
result = norm(p-q)

print("The Euclidean distance between the two Vectors: ",result)

In the above example, we have created a p and q array of the different lengths using numpy library’s random.randint() function.

In this case, python function norm() gives a warning message as two arrays are of different lengths as mentioned below.

//Output
ValueError: operands could not be broadcast together with shapes (90,) (100,)

NOTE:- The norm() function is only applicable for arrays of same length.

Example #3 Euclidean distance between columns in pandas dataframe in python

#import modules
import pandas as pd 
from numpy.linalg import norm

#define DataFrame with three columns
df = pd.DataFrame({'examScore': [88, 85, 76, 70, 92, 94, 89, 85, 90, 93],
                   'studyHours': [4, 3, 6, 5, 4, 5, 8, 7, 4, 6],
                   'Grades': [82, 88, 75, 74, 93, 97, 83, 90, 90, 80]})

print(df)
#calculate Euclidean distance between 'examScore' and 'Grades' 
result = norm(df['examScore'] - df['Grades'])
print("Euclidean distance between 'examScore' and 'Grades': ",result)

Let’s consider a pandas Dataframe with 3 Columns i.e examScore, studyHours, Grades.

Calculate the Euclidean distance between ‘examScore’ and ‘Grades’ dataframe. The norm() functions gives the below output for the above code.

   examScore  studyHours  Grades
0         88           4      82
1         85           3      88
2         76           6      75
3         70           5      74
4         92           4      93
5         94           5      97
6         89           8      83
7         85           7      90
8         90           4      90
9         93           6      80
Euclidean distance between 'examScore' and 'Grades': 17.378147196982766

Above code gives Euclidean distance between ‘examScore’ and ‘Grades’ is 17.378147.

Conclusion

I hope you find the above article on how to calculate Euclidean distance between two points in python code useful and educational.

Leave a Comment