Calculate Eucledian distance in python What is the Euclidean Distance?

Euclidean Distance is the line segment between two points in any dimension. Mathematically, we can evaluate it as the square root of the sum of squares of the difference between the given points.

Ways to Calculate the Euclidean distance in Python

Let us discuss how to calculate this value using different methods from different modules in Python.

Using the scipy.spatial.distance.euclidean() function

The scipy module can perform a variety of scientific calculations. The scipy.spatial.distance.euclidean() function is the most direct method to calculate the Euclidean distance and implements its formula internally for two points.

For example,

Output:

7.280109889280518

Using the math.dist() function

The math module has functions that can perform different mathematical calculations in Python. The math.dist() function calculates the Euclidean distance between two given points.

For example,

Output:

7.280109889280518

Using the numpy.sqrt(), numpy.square(), numpy.sum() functions

We can apply the discussed formula of calculating the Euclidean distance using these three functions.

We use methods from the numpy module when we have the coordinates stored in an array.

See the following code.

Output:

7.280109889280518

In the above example,

• The numpy.square() function returns the square of the given array elements.
• We calculate their sum using the numpy.sum() function.
• The numpy.sqrt() function will calculate the square root of this value, that is essentially the Euclidean distance.

Using the numpy.linalg.norm() function

The numpy.linalg.norm() function calculates the vector norm of a given array. This value is essentially the same as the Euclidean distance.

See the code below.

Output:

7.280109889280518

Using the numpy.dot() function

We use the numpy.dot() function to calculate the dot product of two arrays.

We can use this function to implement the mathematical formula for the Euclidean distance.

For example,

Output:

7.280109889280518

In the above example,

• We create an array that calculates the difference between the two points.
• The numpy.dot() function calculates the dot product between this array and its transpose (calculated using .T).
• The numpy.sqrt() function returns the square root of the final value.

Conclusion

We discussed different methods to calculate the Euclidean Distance using the math, numpy, and scipy modules. The numpy methods work on arrays and can be a little slow. The remaining two, scipy and math modules, execute faster. In these two modules, the points need not be in the form of an array.

import_contacts

You may also like: Related Posts

• 17 January

How to copy file to another directory in Python

Table of ContentsWays to copy file to another directoy in PythonUsing file handlingUsing the shutil libraryUsing the pathlib libraryUsing the os moduleUsing the subprocess moduleConclusion In this article, we will see different ways to copy file to another directory in Python. We can read and write files in Python. We can also work with paths […]

• 13 January

Check if variable is String in python

Table of ContentsHow to check if a given variable is of the string type in Python?Using the isinstance() function.Using the type() function.Check if function parameter is String In this post, we will see what is a string in Python and how to check whether a given variable is a string or not. There are many […]

• 13 January

Count number of characters in a string in python

Table of ContentsWays to count the number of characters in a string in PythonUsing the len() functionUsing the for loopUsing the collections.Counter classConclusion In this post, we will see how to count number of characters in a String in Python. We can think of strings as a collection of characters, with every character at a […]

• 12 January

Pandas apply function to column

Table of ContentsHow do I apply function to column in pandas?Using dataframe.apply() functionUsing lambda function along with the apply() functionUsing dataframe.transform() functionUsing map() functionUsing NumPy.square() function We make use of the Pandas dataframe to store data in an organized and tabular manner. Sometimes there, is a need to apply a function over a specific column […]

• 12 January

How to Copy File in Python?

Table of ContentsCopy file using the shutil module in PythonCopy file using the copyfileobj() function in PythonCopy file using the copyfile() function in PythonCopy file using the copy() function in PythonCopy file using the copy2() function in PythonCopy file using the operating system command in PythonCopy file using the os module in PythonCopy file using […]

• 11 January

What is % in Python?

Table of ContentsWhat is % Operator in Python?The % Operator with strings in pythonThe % Operator as a placeholder for variables  in pythonThe % Operator as format specifiers in pythonConclusion In this article, we will cover what is % in Python and what are different ways to use % in Python. In python, there are […]

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.