Calculating Euclidean distance using SciPy


Euclidean distance is the distance between two real-valued vectors. Mostly we use it to calculate the distance between two rows of data having numerical values (floating or integer values). Below is the formula to calculate Euclidean distance −

$$\mathrm{d(r,s) =\sqrt{\sum_{i=1}^{n}(s_i-r_i)^2} }$$

Here,

r and s are the two points in Euclidean n-space.

si and ri are Euclidean vectors.

n denotes the n-space.

Let’s see how we can calculate Euclidean distance between two points using SciPy library −

Example

# Importing the SciPy library
from scipy.spatial import distance
# Defining the points
A = (1, 2, 3, 4, 5, 6)
B = (7, 8, 9, 10, 11, 12)
A, B
# Computing the Euclidean distance
euclidean_distance = distance.euclidean(A, B)
print('Euclidean Distance b/w', A, 'and', B, 'is: ', euclidean_distance)

Output

((1, 2, 3, 4, 5, 6), (7, 8, 9, 10, 11, 12))

Euclidean Distance b/w (1, 2, 3, 4, 5, 6) and (7, 8, 9, 10, 11, 12) is: 1
4.696938456699069

Updated on: 14-Dec-2021

502 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements