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 ( 21 \right )^{2}+\left ( 21 \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 nonnegative squareroot of an array, elementwise.
Approach :
 Import numpy library and create two numpy arrays representing two points in the plane (say point x and point y).
 Subtract the point x from point y, calculate the square of resultant array.
 Pass the resultant array to the sum() method to get sum of all the elements in the array
 Calculate the square root of the value returned by the sum() method.
 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((xy)*(xy))) 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 :
 Import numpy library and create two numpy arrays representing two points in the plane (say point x and point y).
 Subtract the point x from point y.
 Pass the resultant array to the norm() method.
 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 :
 Import numpy library and create two numpy arrays representing two points in the plane (say point x and point y).
 Subtract the point x from point y this will return an array containing difference of point x and y.
 Now calculate transpose of the diffrence array.
 Pass the resultant array and transpose array to the dot() method.
 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 :
 Import numpy library and create two numpy arrays representing two points in the plane (say point x and point y).
 Subtract the point x from point y this will return an array containing difference of point x and y.
 Now calculate transpose of the diffrence array.
 Pass the resultant array and transpose array to the vdot() method.
 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 :
 Import numpy library and create two numpy arrays representing two points in the plane (say point x and point y).
 Subtract the point x from point y this will return an array containing difference of point x and y.
 Now calculate transpose of the diffrence array.
 Pass the resultant array and transpose array to the multi_dot() method.
 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.
Pandas Tutorials Learn Data Analysis with Python

Pandas Tutorial Part #1  Introduction to Data Analysis with Python

Pandas Tutorial Part #2  Basics of Pandas Series

Pandas Tutorial Part #3  Get & Set Series values

Pandas Tutorial Part #4  Attributes & methods of Pandas Series

Pandas Tutorial Part #5  Add or Remove Pandas Series elements

Pandas Tutorial Part #6  Introduction to DataFrame

Pandas Tutorial Part #7  DataFrame.loc[]  Select Rows / Columns by Indexing

Pandas Tutorial Part #8  DataFrame.iloc[]  Select Rows / Columns by Label Names

Pandas Tutorial Part #9  Filter DataFrame Rows

Pandas Tutorial Part #10  Add/Remove DataFrame Rows & Columns

Pandas Tutorial Part #11  DataFrame attributes & methods

Pandas Tutorial Part #12  Handling Missing Data or NaN values

Pandas Tutorial Part #13  Iterate over Rows & Columns of DataFrame

Pandas Tutorial Part #14  Sorting DataFrame by Rows or Columns

Pandas Tutorial Part #15  Merging or Concatenating DataFrames

Pandas Tutorial Part #16  DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most soughtafter professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.