曼哈顿距离,也称为城市街区距离,计算为两个向量之间的绝对差之和。它主要用于描述统一网格(例如城市街区或棋盘)上的对象的向量。
SciPy 为我们提供了一个名为 cityblock 的函数,它返回两点之间的曼哈顿距离。让我们看看如何使用 SciPy 库计算两点之间的曼哈顿距离 -
# Importing the SciPy library fromscipy.spatialimport distance # Defining the points A = (1, 2, 3, 4, 5, 6) B = (7, 8, 9, 10, 11, 12) print(A, B)输出结果
((1, 2, 3, 4, 5, 6), (7, 8, 9, 10, 11, 12))
# Importing the SciPy library fromscipy.spatialimport distance # Defining the points A = (1, 2, 3, 4, 5, 6) B = (7, 8, 9, 10, 11, 12) # 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