Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Euclidean distance finds the distance between two points in multidimensional space, which is the kind of distance you measure with a ruler. If the points are written as (p1, p2, p3, p4, ...) and (q1, q2, q3, q4, ...), then the formula for Euclidean distance can be expressed as shown in Figure B-1.
A clear implementation of this formula is shown here:
def euclidean(p,q):
sumSq=0.0
# add up the squared differences
for i in range(len(p)):
sumSq+=(p[i]-q[i])**2
# take the square root
return (sumSq**0.5)
Euclidean distance is used in several places in this book to determine how similar two items are.