首页 文章资讯内容详情

使用 SciPy 计算曼哈顿距离

2026-06-02 2 花语

曼哈顿距离,也称为城市街区距离,计算为两个向量之间的绝对差之和。它主要用于描述统一网格(例如城市街区或棋盘)上的对象的向量。

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