Different types of Distance for ML Algorithms

Manhattan, Cosine, Ln Form
# http://dataaspirant.com/2015/04/11/five-most-popular-similarity-measures-implementation-in-python/

# Manhattan
def manhattan(x, y):
return sum( abs(a-b) for a, b in zip(x,y))

d = manhattan([5,10], [30,20])
print(d)

#------ Cosine Distance
import math
def square_rooted(x):
return math.sqrt(sum([a * a for a in x]))

def cosine_distance(x, y):
numerator = sum ( a * b for a, b in zip(x, y ) )
denominator = square_rooted(x) * square_rooted(y)
return numerator/denominator
print ( cosine_distance([3, 45, 7, 2], [2, 54, 13, 15]) )

#----------------
#Ln Form Distance

def ln_form_distance(x,y, dim):
return pow(sum([ pow(abs(a-b),dim) for a,b in zip(x,y)]), 1/dim)

print (ln_form_distance([3, 45, 7, 2], [2, 54, 13, 15], 3))