- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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,

s_{i} and r_{i} 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 Questions & Answers
- Calculating the Hamming distance using SciPy
- Calculating the Minkowski 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
- C program on calculating the amount with tax using assignment operator
- Calculating and adding the parity bit to a binary using JavaScript
- Calculating the area of a triangle using its three sides in JavaScript
- Finding determinant of a square matrix using SciPy library
- Finding inverse of a square matrix using SciPy library
- How to solve banded matrix equations using Python SciPy?
- How to solve triangular matrix equations using Python SciPy?
- How to solve Toeplitz matrix equation using Python SciPy?
- Find the maximum distance covered using n bikes in Python
- Explain OpenCV Distance Transformation using java Example

Advertisements