## 1. Euclidean Distance

Euclidean Distance represents the shortest distance between two points.

Most machine learning algorithms including K-Means use this distance metric to measure the similarity between observations. Let’s say we have two points as shown below:

So, the Euclidean Distance between these two points A and B will be:

Here’s the formula for Euclidean Distance:

We use this formula when we are dealing with 2 dimensions. We can generalize this for an n-dimensional space as:

Where,

- n = number of dimensions
- pi, qi = data points

Let’s code Euclidean Distance in Python. This will give you a better understanding of how this distance metric works.

We will first import the required libraries. I will be using the SciPy library that contains pre-written codes for most of the distance functions used in Python:

# importing the library | |
---|---|

from scipy.spatial import distance | |

# defining the points | |

point_1 = (1, 2, 3) | |

point_2 = (4, 5, 6) | |

point_1, point_2 |

view rawlibrary_and_dataset.py hosted with by GitHub

These are the two sample points which we will be using to calculate the different distance functions. Let’s now calculate the Euclidean Distance between these two points:

# computing the euclidean distance | |
---|---|

euclidean_distance = distance.euclidean(point_1, point_2) | |

print(‘Euclidean Distance b/w’, point_1, ‘and’, point_2, 'is: ', euclidean_distance) |

view raweuclidean_distance.py hosted with by GitHub

This is how we can calculate the Euclidean Distance between two points in Python. Let’s now understand the second distance metric, Manhattan Distance.

## 2. Manhattan Distance

Manhattan Distance is the sum of absolute differences between points across all the dimensions.

We can represent Manhattan Distance as:

Since the above representation is 2 dimensional, to calculate Manhattan Distance, we will take the sum of absolute distances in both the x and y directions. So, the Manhattan distance in a 2-dimensional space is given as:

And the generalized formula for an n-dimensional space is given as:

Where,

- n = number of dimensions
- pi, qi = data points

Now, we will calculate the Manhattan Distance between the two points:

# computing the manhattan distance | |
---|---|

manhattan_distance = distance.cityblock(point_1, point_2) | |

print(‘Manhattan Distance b/w’, point_1, ‘and’, point_2, 'is: ', manhattan_distance) |

view rawmanhattan_distance.py hosted with by GitHub

Note that **Manhattan Distance is also known as city block distance.** SciPy has a function called *cityblock* that returns the Manhattan Distance between two points.

Let’s now look at the next distance metric – Minkowski Distance.