Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

B. Mathematical Formulas > Euclidean Distance

Euclidean Distance

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.

Euclidean distance

Figure B-1. Euclidean distance

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.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint