Calculate Euclidean distance using NumPy in Python

In this article we will learn How to calculate euclidean distance using numpy in Python.

Table Of Contents

What is euclidean distance?

Euclidean distance is the distance between two points in a plane.

Given Two points on a plane we need to calculate Euclidean Distance between them.

Example : Euclidean distance between the points (1,1) and (2,2)

\sqrt{\left ( 2-1 \right )^{2}+\left ( 2-1 \right )^{2}}
\sqrt{1+1}
\sqrt{2}

There are multiple ways for calculating Euclidean Distance. Lets discuss all of the methods one by one with proper approach and a working code examples.

Calculate euclidean distance using sqrt() and sum() methods of numpy

The numpy module have sqrt() and sum() functions.

The sqrt() function is used to get the square root of a number. Whereas, the sum() function will return the sum of all the elements present in a sequence. Now we will implement the formula for calculating euclidean distance using sum() and sqrt().

Syntax of sqrt() function

numpy.sqrt(x)
  • Parameters:
  • x : x is array of numbers whose square root to be calculated.
  • Returns:
  • Return the non-negative square-root of an array, element-wise.

Approach :

  1. Import numpy library and create two numpy arrays representing two points in the plane (say point x and point y).
  2. Subtract the point x from point y, calculate the square of resultant array.
  3. Pass the resultant array to the sum() method to get sum of all the elements in the array
  4. Calculate the square root of the value returned by the sum() method.
  5. Resultant of the square root will be the euclidean distance.

Source Code

import numpy as np

# create 2 numpy arrays which are considered as the point x and point y
x = np.array([7, 8, 9])
y = np.array([9, 8, 7])

# calculating the euclidean  distance
distance = np.sqrt(np.sum((x-y)*(x-y)))

print(distance)

Output :

2.8284271247461903

Calculate euclidean distance using norm() function

The numpy library has a linalg module which consists of multiple linear algebric functions and norm() is one of them. It is generally used to calculate the error in the models i.e, The diffrence between the predicted and actual value, in other words distance between two points. To caluclate the euclidean distance we need to pass the diffrence between two points to the norm() method and it will return the euclidean distance.

Syntax of norm() function

numpy.linalg.norm(x)
  • Parameters:
  • x : array_like
  • Returns:
  • float .

Approach :

  1. Import numpy library and create two numpy arrays representing two points in the plane (say point x and point y).
  2. Subtract the point x from point y.
  3. Pass the resultant array to the norm() method.
  4. The value returned by the norm() method will be the euclidean distance.

Source Code

import numpy as np

# create 2 numpy arrays which are considered as the point x and point y
x = np.array([7, 8, 9])
y = np.array([9, 8, 7])

# calculating the euclidean  distance
distance = np.linalg.norm(x - y)

# printing euclidean  distance
print(distance)

Output :

2.8284271247461903

Calculate euclidean distance using dot()

The numpy module has a dot() function. It is used to get the Dot product of two arrays. So to calculate the euclidean distance we need to pass the array containing diffrence of two points and transpose of the array, The dot() will return the dot product of those two arrays. Now apply square root to the resultant value.

Syntax of dot() function

numpy.dot(arr1, arr2)
  • Parameters:
  • arr1 : first array to be passed
  • arr2 : second array to be passed
  • Returns:
  • Dot product of two arrays passed.

Approach :

  1. Import numpy library and create two numpy arrays representing two points in the plane (say point x and point y).
  2. Subtract the point x from point y this will return an array containing difference of point x and y.
  3. Now calculate transpose of the diffrence array.
  4. Pass the resultant array and transpose array to the dot() method.
  5. Calculate square root for the value returned by the dot() method, this will be the euclidean distance.

Source Code

import numpy as np

# create 2 numpy arrays which are considered as the point x and point y
x = np.array([7, 8, 9])
y = np.array([9, 8, 7])

# calculating the euclidean  distance

x_minus_y = x - y
x_minus_y_transpose = x_minus_y.T
distance = np.sqrt(np.dot(x_minus_y, x_minus_y_transpose))


# printing euclidean  distance
print(distance)

Output :

2.8284271247461903

Calculate euclidean distance using vdot()

The numpy module has a vdot() function, and it is used to get the Dot product of two arrays. So to calculate the euclidean distance we need to pass the array containing diffrence of two points and transpose of the array, The vdot() will return the dot product of those two arrays. Now apply square root to the resultant value.

Syntax of vdot() function

numpy.vdot(arr1, arr2)
  • Parameters:
  • arr1 : first array to be passed
  • arr2 : second array to be passed
  • Returns:
  • Dot product of two arrays passed.

Approach :

  1. Import numpy library and create two numpy arrays representing two points in the plane (say point x and point y).
  2. Subtract the point x from point y this will return an array containing difference of point x and y.
  3. Now calculate transpose of the diffrence array.
  4. Pass the resultant array and transpose array to the vdot() method.
  5. Calculate square root for the value returned by the vdot() method, this will be the euclidean distance.

Source Code

import numpy as np

# create 2 numpy arrays which are considered as the point x and point y
x = np.array([7, 8, 9])
y = np.array([9, 8, 7])


# calculating the euclidean  distance

x_minus_y = x - y
x_minus_y_transpose = x_minus_y.T
distance = np.sqrt(np.vdot(x_minus_y, x_minus_y_transpose))


# printing euclidean  distance
print(distance)

Output :

2.8284271247461903

Calculate euclidean distance using np.linalg.multi_dot()

The numpy library have linalg module which consists of multi_dot() function. It is used to get the Dot product of arrays. So to calculate the euclidean distance we need to pass the array containing diffrence of two points and transpose of the array. The vdot() will return the dot product of those two arrays. Now apply square root to the resultant value.

Syntax of multi_dot() function

np.linalg.multi_dot(arrays)
  • Parameters:
  • arrays : sequence of arrays for which dot product need to be calculated.
  • Returns:
  • Dot product of arrays passed.

Approach :

  1. Import numpy library and create two numpy arrays representing two points in the plane (say point x and point y).
  2. Subtract the point x from point y this will return an array containing difference of point x and y.
  3. Now calculate transpose of the diffrence array.
  4. Pass the resultant array and transpose array to the multi_dot() method.
  5. Calculate square root for the value returned by the multi_dot() method, this will be the euclidean distance.

Source Code

import numpy as np

# create 2 numpy arrays which are considered as the point x and point y
x = np.array([7, 8, 9])
y = np.array([9, 8, 7])

# calculating the euclidean  distance

x_minus_y = x - y
x_minus_y_transpose = x_minus_y.T

distance = np.sqrt(np.linalg.multi_dot([x_minus_y, x_minus_y_transpose]))


# printing euclidean  distance
print(distance)

Output :

2.8284271247461903

Summary

Great! you made it, We have discussed multiple methods to calculate the euclidean distance in numpy. Happy learning.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top