How to compute the Manhattan distance in Python numpy
The Manhattan distance is the distance computed in terms of “city blocks” between two locations. In contrast to the Euclidean distance which is measuring the “as the crow flies” distance (by assuming you can directly fly between locations), the Manhattan distance assumes that you can just go horizontally or vertically along city blocks and counts the number of blocks traversed as the distance measure.
The mathematical definition of the Manhattan distance involves taking the absolute value of the differences between respective coordinates and adding them up (as shown in the below figure).
Method 1: Compute Manhattan distance using abs() and sum() functions
Here is a simple way to compute the Manhattan distance. We simply code up the above formula using basic numpy functions, like so:
In the above code, we are first creating two locations at coordinates (-1,1) and (2,-3). Note that the x-coordinates are off by a unit of 3 (i.e., 2 minus -1) and the y-coordinates are off by a unit of 4 (-3 minus 1). The exact signs don’t matter because we are taking the absolute values of these differences and adding them (using np.abs and then np.sum). This returns a single number, a scalar which is the Manhattan distance.
If we run this program, we get:
This makes sense because the two differences were 3 and 4, and 3 and 4 added up gives you 7.
Method 2: Compute Manhattan distance using the scipy package and cityblock() function
A second way to compute the Manhattan distance is to use the scipy package and the cityblock() function within it. This works as follows:
Note that in the above code we simply pass location1 and location2 as two arguments to the cityblock() function. The output is the same as before:
We have learnt two different ways to compute the Manhattan distance using the Python numpy module. Which one is your favorite?
Finally, note that we have used points in two dimensional space (like latitude, longitude) and computed distances between them. But the same codes above will work even if your points are in a higher dimensional space, like 3 or higher.
If you liked this blogpost, you should explore a different way to compute distances such as the Euclidean distance.
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.