SciPy - maxdists() 方法
SciPy maxdists() 方法引用模块"from scipy.spatial.distance import pdist",该模块允许函数 pdist() 计算给定集合中点之间的成对距离。
此函数在 scipy 中没有任何直接方法。如果用户想要找到最大成对距离,他们可能会更喜欢返回所有成对距离的方法 pdist()。
语法
其语法如下 −
pdist(point)
参数
此方法仅接受单个参数 −
- point:这是一个使用 array() 存储点数组的简单变量。
返回值
此方法返回整数和浮点值的第 nd 个数组。
示例 1
以下是 SciPy maxdists() 方法,它说明了使用函数 pdist() 和 max()。
import numpy as np from scipy.spatial.distance import pdist # 输入数据 point = np.array([ [0, 0], [1, 1], [2, 2], [3, 3], [4, 4] ]) # 成对距离 dist = pdist(point) # 最大距离 max_dist = np.max(dist) print("成对距离的结果:", dist) print("最大距离的结果:", max_dist)
输出
上述代码产生以下输出 −
成对距离的结果:[1.41421356 2.82842712 4.24264069 5.65685425 1.41421356 2.82842712 4.24264069 1.41421356 2.82842712 1.41421356] 最大距离的结果: 5.656854249492381
示例 2
此程序执行与示例 1类似的相同计算,但在这里您将获得不同距离度量的结果。
import numpy as np from scipy.spatial.distance import pdist # input data in the form of array of points point = np.array([ [0, 0], [1, 1], [2, 2], [3, 3], [4, 4] ]) # 使用曼哈顿(城市街区)距离计算成对距离 dist = pdist(point, metric='cityblock') # 找到最大距离 max_dist = np.max(dist) print("Pairwise distances (Manhattan):", dist) print("Maximum distance (Manhattan):", max_dist)
输出
上述代码产生以下输出−
Pairwise distances (Manhattan): [2. 4. 6. 8. 2. 4. 6. 2. 4. 2.] Maximum distance (Manhattan): 8.0