Manhattan Distance is extremely similar to Euclidean distance. Rather than summing the squared difference between each dimension, we instead sum the absolute value of the difference between each dimension. It’s called Manhattan distance because it’s similar to how you might navigate when walking city blocks. If you’ve ever wondered “how many blocks will it take me to get from point A to point B”, you’ve computed the Manhattan distance.
The equation is shown below:
Note that Manhattan distance will always be greater than or equal to Euclidean distance. Take a look at the image below visualizing Manhattan Distance:
euclidean_distance(), create a function called
manhattan_distance() that takes two lists named
pt2 as parameters.
In the function, create a variable named
distance, set it equal to
0, and return it.
distance, create a
for loop to loop through the dimensions of each point.
Add the absolute value of the difference between each dimension to
Remember, in Python, you can take the absolute value of
num by using
You’re done with
manhattan_distance()! Go ahead and find the Manhattan distance between the same points as last time.
Below the print statements for Euclidean distance, print the Manhattan distance between
[1, 2] and
Also print the Manhattan distance between
[5, 4, 3] and
[1, 7, 9].