- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- 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 −
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−
# 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)
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
- 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?