Python Euclidean Distance
Euclidean distance is the distance between two points with whatever of dimensions. We are using NumPy library to find and calculate the Euclidean distance. The NumPy library is used for manipulate 2-dimensional or more than 2-dimensional array in a systematic way. There are few methods to find Euclidean distance using NumPy library.
Method 1: using dot ()
// python program to calculate Euclidean distance using dot ()
importnumpy as y
//declaring points in arrays
Point11 =y.array((1, 2, 3))
point22 =y.array((1, 1, 1))
// subtracting vector
temp =point11–point22
//we are finding sum of squares using dot product
sum_sq =y.dot(temp.T, temp)
//then doing squareroot and displaying value of Euclidean distance
print(y.sqrt(sum_sq))
Output:
2.23606797749979
Method 2: using linalg.norm ()
// python program to calculate Euclidean distance using
linalg.norm ()
importnumpy as y
// declaring points in numpy arrays
point11 =y.array(1, 2, 3)
point22 =y.array(1, 1, 1)
//evaluating Euclidean distance using linalg.norm()
dist =y.linalg.norm(point11 -point22)
//displaying Euclidean distance
print(dist)
Output:
2.23606797749979
Method 3: using sum () and square ()
// python program to calculate Euclidean distance using square () and sum ()
importnumpy as y
// declaring points in arrays
point11 =y.array(1, 2, 3)
point22 =y.array(1, 1, 1)
// finding sum of squares
sum_sq =y.sum(y.square(point11–point22))
//then doing squareroot and displaying Euclidean distance
print(y.sqrt(sum_sq))
Output:
2.23606797749979