python 与scipy计算距离:欧几里得,曼哈顿,切比雪夫

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 与scipy计算距离:欧几里得,曼哈顿,切比雪夫相关的知识,希望对你有一定的参考价值。

import os
import sys
from scipy.spatial import distance

# Find the AIFH core files
aifh_dir = os.path.dirname(os.path.abspath(__file__))
aifh_dir = os.path.abspath(aifh_dir + os.sep + ".." + os.sep + "lib" + os.sep + "aifh")
sys.path.append(aifh_dir)

# Create three different positions.
pos1 = [1.0, 2.0, 3.0]
pos2 = [4.0, 5.0, 6.0]
pos3 = [7.0, 8.0, 9.0]


# Calculate the distance between the specified points in 3 metrics.
print("Euclidean Distance")
print("pos1->pos2: " + str(distance.euclidean(pos1, pos2)))
print("pos2->pos3: " + str(distance.euclidean(pos2, pos3)))
print("pos3->pos1: " + str(distance.euclidean(pos3, pos1)))
print("\nManhattan (city block) Distance\n")
print("pos1->pos2: " + str(distance.cityblock(pos1, pos2)))
print("pos2->pos3: " + str(distance.cityblock(pos2, pos3)))
print("pos3->pos1: " + str(distance.cityblock(pos3, pos1)))
print("\nChebyshev Distance\n")
print("pos1->pos2: " + str(distance.chebyshev(pos1, pos2)))
print("pos2->pos3: " + str(distance.chebyshev(pos2, pos3)))
print("pos3->pos1: " + str(distance.chebyshev(pos3, pos1)))

以上是关于python 与scipy计算距离:欧几里得,曼哈顿,切比雪夫的主要内容,如果未能解决你的问题,请参考以下文章

常用距离计算方法:

欧几里德距离,Scipy,纯Python和Java之间的结果不同

Python中的多维欧几里得距离

相似系数常用的有哪几种

机器学习中“距离与相似度”计算汇总

python中的测地距离变换