- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculating the Manhattan distance using SciPy
The Manhattan distance, also known as the City Block distance, is calculated as the sum of absolute differences between the two vectors. It is mostly used for the vectors that describe objects on a uniform grid such as a city block or chessboard. Below is the generalized formula to calculate Manhattan distance in n-dimensional space −
$$\mathrm{D =\sum_{i=1}^{n}|r_i-s_i|}$$
Here,
si and ri are data points.
n denotes the n-space.
SciPy provides us with a function named cityblock that returns the Manhattan Distance between two points. Let’s see how we can calculate the Manhattan distance between two points using SciPy library−
Example
# Importing the SciPy library from scipy.spatial import distance # Defining the points A = (1, 2, 3, 4, 5, 6) B = (7, 8, 9, 10, 11, 12) A, B # Computing the Manhattan distance manhattan_distance = distance.cityblock(A, B) print('Manhattan Distance b/w', A, 'and', B, 'is: ', manhattan_distance)
Output
Manhattan Distance b/w (1, 2, 3, 4, 5, 6) and (7, 8, 9, 10, 11, 12) is: 36
- Related Articles
- Calculating the Minkowski distance using SciPy
- Calculating the Hamming distance using SciPy
- Calculating Euclidean distance using SciPy
- Count paths with distance equal to Manhattan distance in C++
- Program to generate matrix where each cell holds Manhattan distance from nearest 0 in Python
- Calculating profit or loss using Python
- Finding determinant of a square matrix using SciPy library
- Finding inverse of a square matrix using SciPy library
- How to solve triangular matrix equations using Python SciPy?
- C program on calculating the amount with tax using assignment operator
- Calculating and adding the parity bit to a binary using JavaScript
- How to implement ‘cubic’ 1-D interpolation using SciPy library?
- Comparing ‘cubic’ and ‘linear’ 1-D interpolation using SciPy library
- How to solve a circulant matrix equation using Python SciPy?
- How to find the Eigenvalues and Eigenvectors of a square matrix using SciPy?

Advertisements