使用 SciPy 计算曼哈顿距离
曼哈顿距离,也称为城市街区距离,计算为两个向量间绝对差值的总和。它通常用于描述均匀网格上对象的向量,例如城市街区或棋盘格。以下是 n 元空间中计算曼哈顿距离的通用公式 −
$$\mathrm{D =\sum_{i=1}^{n}|r_i-s_i|}$$
其中:
si 和 ri 为数据点。
n 表示 n 元空间。
SciPy 为我们提供了一个名为cityblock 的函数,用于返回两点间的曼哈顿距离。让我们看看如何使用 SciPy 库计算两点间的曼哈顿距离−
示例
# 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP