• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Numpy Tips / How to Compute Manhattan Distance in Python with Numpy

How to Compute Manhattan Distance in Python with Numpy

December 27, 2022 by cmdlinetips

In this post, we will learn how to compute Manhattan distance, one. of the commonly used distance meeasures, in Python using Numpy. Manhattan distance is also known as the “taxi cab” distance as it is a measure of distance between two points in a grid-based system like layout of the streets in Manhattan, New York City. It is calculated by determining the vertical and horizontal distance between two points and then adding them together.

We can define Manhattan distance as the sum of the absolute differences of the coordinates between two points in a grid. For example, if we have two points (x1, y1) and (x2, y2), the Manhattan distance would be:

|x1 - x2| + |y1 - y2|
Computing Manhattan Distance with Numpy
Computing Manhattan Distance with Numpy

First, let’s start importing Numpy.

import numpy as np

. Computing Manhattan distance between 2D points in Python

Let us compute Manhattan distance between two points shown in the aabove figure illustrating the Manhattan distance.

point1 = np.array([0, 0])
point2 = np.array([4, 4])
print(point1)
print(point2)

We first compute the differences between two points.

diff = point1 - point2
diff
array([-4, -4])

Then we compute the absolute values of the differences.

abs_diff = np.abs(diff)
abs_diff
array([4, 4])

Then we add them up to get the Manhattan distance. For the example we have the Manhattan distance is 8.

manhattan_dist = np.sum(abs_diff)
manhattan_dist

8

We can use the above steps in a single statement and compute Manhattan distance. For our toy example in the figure, the Manhattan distance is 8 and we can visually verify that as well.

# Calculate the Manhattan distance in 2D
manhattan_distance = np.sum(np.abs(point1-point2))
print(manhattan_distance)  

8

Here is another example of computing Manhattan distance between two 2D points.

point1 = np.array([1, 2])
point2 = np.array([4, 6])
# Calculate the Manhattan distance in 2D
manhattan_distance = np.sum(np.abs(point1-point2))
print(manhattan_distance) 

7

<h3. Computing Manhattan distance between two points in 3D
We can use the same approach to compute distance between two points in 2-dimension. For example, the Manhattan distance between two points (x1,y1,z1) and (x2,y2,z2) in 3D is

abs(x1-x2) + abs(y1-y2) + abs(z1-z2) 

Let us create some data for points in 3D.

point1 = np.array([1, 2, 3])
point2 = np.array([4, 6, 8])

We can compute Manhattan distance by summing the absolute values of differences between points.

manhattan_distance = np.sum(np.abs(point1-point2))
manhattan_distance

12

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow to Compute Euclidean distance in Numpy Default Thumbnail3 Basic Commands to Manipulate NumPy 2d-arrays NumPy is the fundamental package for scientific computing with Python. It contains among other things: a powerful N-dimensional array object sophisticated (broadcasting) functions tools for integrating C/C++ and Fortran code useful linear algebra, Fourier transform, and random number capabilities10 Basic Arithmetic Operations with NumPy array Default Thumbnail12 Basic Commands with NumPy Array

Filed Under: Manhattan distance, Numpy Tips Tagged With: Manhattan distance Numpy

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version