Some Euclidean Distances

$ L_2 $ norm

  • d(x,y) = square root of the sum of the squares of the differences between x and y in each dimension.
  • The most common notion of "distance"
$$ d(x, y) = \sqrt {(x_2 - x_1)^2 + (y_2 - y_1) ^2} $$

$ L_1 $ norm

  • sum of the differences in each dimension.
    • Manhattan distance = distance if you had to travel along coordinates only.
$$ d(x, y) = (x_2 - x_1) + (y_2 - y_1) $$

Example:


In [6]:
import math
# L1-norm & L2-norm distances

def l1_norm(x1, y1, x2, y2):
    """ Calculate Euclidean L1 norm distance
    
    Keyword Arguments
    x1,y1 -- co-ordinates of point 1
    x2,y2 -- co-ordinates of point 2
    """
    
    distance = (x2 - x1) + (y2 - y1)
    return distance

def l2_norm(x1, y1, x2, y2):
    """ Calculate Euclidean L2 norm distance
    
    Keyword Arguments
    x1,y1 -- co-ordinates of point 1
    x2,y2 -- co-ordinates of point 2
    """
    
    distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
    return distance

In [7]:
# testing the distance functions

print 'Manhattan Distance: ', l1_norm(5,5,9,8)
print 'L2-norm Distance: ', l2_norm(5,5,9,8)


Manhattan Distance:  7
L2-norm Distance:  5.0

In [23]:
print 'L1-norm Distance to (0,0) : ', l1_norm(0,0,56,13)
print 'L1-norm Distance to (100,40) : ', l1_norm(100,40,56,13)


print 'L2-norm Distance to (0,0) : ', l2_norm(0,0,56,13)
print 'L2-norm Distance to (100,40) : ', l2_norm(100,40,56,13)


L1-norm Distance to (0,0) :  69
L1-norm Distance to (100,40) :  -71
L2-norm Distance to (0,0) :  57.4891294072
L2-norm Distance to (100,40) :  51.6236379966

In [ ]: