Euclidean Distance, Manhattan distance and Minkowski distance in Python

Question

Given two objects represented by the tuples (22, 1, 42, 10) and (20, 0, 36, 8):
(a) Compute the Euclidean distance between the two objects.
(b) Compute the Manhattan distance between the two objects.
(c) Compute the Minkowski distance between the two objects, using q = 3.
(d) Compute the supremum distance between the two objects.

Solved answer using python 3.

'''
Problem :
Given two objects represented by the tuples (22, 1, 42, 10) and (20, 0, 36, 8):
(a) Compute the Euclidean distance between the two objects.
(b) Compute the Manhattan distance between the two objects.
(c) Compute the Minkowski distance between the two objects, using q = 3.
(d) Compute the supremum distance between the two objects.
Author : Sounak Pal'''

from scipy.spatial import distance

#tup1=(22, 1, 42, 10)
#tup2=(20, 0, 36, 8)
list1=[]
list2=[]

for i in range(0, 4):
    inp1=int(input("enter tuple 1:\t"))
    list1.append(inp1)

for i in range(0, 4):
    inp2=int(input("enter tuple 2:\t"))
    list2.append(inp2)

tup1=tuple(list1)
tup2=tuple(list2)

print(tup1,tup2)

edis = distance.euclidean(tup1, tup2)
print("Euclidean distance :\t ", edis)

mDis= distance.cityblock(tup1,tup2)
print("Manhattan distance :\t ", mDis)

minDis= distance.wminkowski(tup1,tup2,3,1)
print("Minkowski Distance :\t", minDis)

supDis= distance.chebyshev(tup1,tup2)
print("Supremum  Distance :\t", supDis)

 

Algorithm For this programme 

Input: Data set of objects as list1and list2
Output: Displaying Euclidean, Manhattan, Minkowski and supremum distance
between the two objects.
Data Structure: list1 and list2 are 2 lists where we store user-inputted data. Tup1 and tup2
are 2 tuples which represented the 2 two objects
Description: Using scipy package’s function to calculate the distance. Using scipy’s
function Euclidean, Manhattan, wminkowski and chebyshev to find the distance between the two
objects

Step 1 : Start
Step 2 : Display “ enter tuple 1”
                Read list1
Step 3 : Display “enter tuple 2”
                 Read list2
Step 4 : Turn list1 into a tuple called tup1
Step 5 : Turn list2 into a tuple called tup2
Step 6 : Display tup1 and tup2
Step 7 : edis ← call the euclidean function of scipy package with tup1 and tup2
                Display “Euclidean distance ” edis
Step 8 : mDis ← call the cityblock function of scipy package with tup1 and tup2
                 Display “Manhattan distance” mDis
Step 9 : minDis ← call the wminkowski function of scipy package with tup1 and tup2
                 Display “Minkowski Distance” minDis
Step 10 : supDis ← call the chebyshev function of scipy package with tup1 and tup2
                   Display “Supremum Distance” supDis
Step 11 : Stop 

Leave a Comment

Your email address will not be published. Required fields are marked *