Euclidean Distance Python: Easy Beginner’s Guide in 2020

Introduction

In mathematics, the Euclidean Distance, also known as Euclidean metric, is a distance between two points in the Euclidean space that can be measured with a ruler and is given by the Pythagorean formula. By the use of this formula as distance, Euclidean space becomes a metric space. And, the norm associated is called the Euclidean norm.

In this article, we will discuss the different types of Euclidean dimensional spaces with formulas to calculate them. We will also see an example of each dimensional space to understand the calculation. Let us learn more about euclidean distance python.

  1. How to Calculate Euclidean Distance?
  2. What is Squared Euclidean Distance? 

1. How to Calculate Euclidean Distance? 

Euclidean Distance Python is easier to calculate than to pronounce! 

To measure Euclidean Distance in Python is to calculate the distance between two given points. These given points are represented by different forms of coordinates and can vary on dimensional space. Finding the Euclidean Distance in Python between variants also depends on the kind of dimensional space they are in.

A) Here are different kinds of dimensional spaces: 

  • One-dimensional space: In one-dimensional space, the two variants are just on a straight line, and with one chosen as the origin. The length of the line between these two given points defines the unit of distance, whereas the direction from the origin to the other point is called a positive direction.
  • Two-dimensional space: In two-dimensional space, the variants and their coordinates are given as the points on the x-axis and y-axis. 
  • Three-dimensional space: Similarly, in three-dimensional space, the variants and their coordinates are given as the points on the x, y, and z-axis.

B) Following are the  different Euclidean formulas based on the dimensional spaces are:

  • For one-dimensional space: 

The Euclidean formula used for calculating Euclidean Distance in Python for one-dimensional space is  

 (q-p)²=|q-p| 

  • For two-dimensional space: 

The Euclidean formula used for calculating Euclidean Distance in Python for two-dimensional space is 

 (q1-p1)² +(q2-p2)² =d(q,p) 

  • For three-dimensional space: 

The formula used for calculating Euclidean Distance for three-dimensional space is 

 (q1-p1)² +(q2-p2)²+(q3-p3)² =d(q,p) 

C) Calculating the Euclidean Distance between Two Points or for One-Dimensional Space: 

To calculate the Euclidean Distance between two points or for one-dimensional space using the (q-p)²=|q-p| formula, firstly, subtract one point on the number line from the other one. The order of the subtraction, in this case, doesn’t matter and you can subtract ‘q’ from ‘p’ or vice-versa. 

Now, calculate the absolute value of the difference. To calculate the absolute value, square the answer that came after subtracting the digits. Do a square root of the last answer you got. The remainder left is the Euclidean Distance between two points.

An example of one-dimensional space calculation:

For example, in a one-dimensional space, let’s consider one number as eight and the other as -3. Subtract 8 from -3, and you will get  -11. To find the absolute value, we will square the number -11, which will be equal to 121. Now the final step will be to calculate the square root of 121, i.e. 11. The Euclidean Distance between two points is 11.

D) Calculating the Euclidean Distance for Two-Dimensional Space: 

To calculate the Euclidean Distance for two-dimensional space using the  (q1-p1)² +(q2-p2)² =d(q,p) formula, firstly, subtract the coordinates of the first point (q1, q2) to the coordinates of the second point (p1,p2). Now follow the same pattern that we did in one-dimensional space calculation, i.e. do a square of both the numbers and add them. After adding, calculate the absolute value of the remainder by finding its square root. The remainder left is the Euclidean Distance for two-dimensional space.

An example of two-dimensional space calculation:

For example, in two-dimensional space, let’s consider one coordinate as (2, 4) and the other as (-3, 8). Now subtracting the coordinates of first to the second, we will get (2-(-3))²+(4-8)²=(-5)² +(-4)². To find the absolute value, we will square the numbers, which will be equal to 25+16=41. Now the final step will be to calculate the square root of 41, i.e. 6.40. The Euclidean Distance between the two-dimensional space is 6.4.

E) Calculating the Euclidean Distance for Three-Dimensional Space: 

To calculate the Euclidean Distance for three-dimensional space using the (q1-p1)² +(q2-p2)²+(q3-p3)² =d(q,p) formula, firstly, subtract the coordinates of the first point (q1,q2,q3) to the coordinates of the second point (p1,p2,p3). Now follow the same pattern that we did in one-dimensional and two-dimensional space calculation, i.e. do a square of all the three numbers and add them. After adding, calculate the absolute value of the remainder by finding its square root. The remainder left is the Euclidean Distance for three-dimensional space.

An example of three-dimensional space calculation:

For example, in three-dimensional space, let’s consider one coordinate as (3, 6, 5) second as (7, -5, 1). Now subtracting the coordinates of first to the second, we will get (3-7)²+(-5-6)²+(5-1)²=(-4)² +(-11)²+(4)². To find the absolute value, we will square the numbers, which will be equal to 16+121+16=153. Now the final step will be to calculate the square root of 153, i.e. 12.36. The Euclidean Distance between three-dimensional space is 12.36.

2. What is Squared Euclidean Distance? 

The squared Euclidean Distance formula is used to calculate the distance between two given points a and b, with k dimensions, where k is the number of measured variables.

d2 (a,b)=(a1-b1)2+(a2-b2)2+(a3-b3)2…………+(ak-bk)2

The formula that is used for calculating the squared Euclidean Distance is j=1k(aj-bj)2.

Here’s a tip:

To split s = “a,b,c1”,

Use this formula s.split(“,”)

[“a”, “b”, “c”]

Conclusion 

The Euclidean Distance calculation method is as easy as it seems here. However, the traditional method may not be considered optimal for computer graphics, simulations, and video game development because of its dependence on the square root operation, which many times can be prohibitively slow in work. 

If you are a Python enthusiast and want to learn more about it, Jigsaw Academy’s Full Stack Data Science Program, an online 6-month course with industry-validated & recommended curriculum by SSC NASSCOM is perfect for you! 

Also Read

Related Articles

loader
Please wait while your application is being created.
Request Callback