Tuesday, January 30, 2018

Euclidean Distance using Python


As per Wikipedia , the Euclidean distance or Euclidean metric is the "ordinary" straight-line distance between two points in Euclidean space.

The euclidean distance between the point A and B will be the line segment joining the point AB.

In a simple 2 dimensional space  , euclidean distance can be calculated by the below formula:

Let P is a point  with co-ordinates as (p1,p2) and Q is another point with co-ordinates as (q1,q2).
Then , the Euclidean Distance PQ,.



Similarly , for a point in 3 dimensional space , with coordinates as P(p1,p2,p3) and Q(q1,q2,q3)
 the distance is,


Let us take a simple example to understand it :-

(Euclidean) distance between points (2, -1) and (-2, 2) is found like this:-



Python implementation of Euclidean Distnace using scipy:-

from scipy.spatial import distance
a = (1,2,3)
b = (4,5,6)
dst = distance.euclidean(a,b)
print(dst)

#output :-5.19615242271

Further reading :-

https://en.wikipedia.org/wiki/Euclidean_distance

https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.spatial.distance.euclidean.html


No comments:

Post a Comment

Hadoop - What is a Job in Hadoop ?

In the field of computer science , a job just means a piece of program and the same rule applies to the Hadoop ecosystem as wel...