JGuizard
6/12/2016 - 5:34 PM

Compute distance with scipy: Euclidean, Manhattan, Chebyshev

Compute distance with scipy: Euclidean, Manhattan, Chebyshev

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)))