Euclidean Distance is a way to measure the straight-line Distance between two points in a multidimensional space. The Distance is always zero or positive. It is based on the famous Pythagoras theorem.
For example, if you have two points on a piece of paper, the Euclidean Distance is the length of the shortest line you can draw to connect those two points.
You can use Euclidean distance in mathematics, physics, and computer science.
Here are three ways to calculate Euclidean distance using Numpy:
- Using np.linalg.norm()
- Using np.sqrt() and np.sum()
- Using np.dot()
Method 1: Using np.linalg.norm()
The efficient and easiest way to calculate Euclidean distance (which is the L2 norm) is by default, which is equivalent to the sum of the square root of squares sum of squares.
import numpy as np # Defining two points in space A = np.array([19, 21, 30]) B = np.array([41, 51, 61]) # Calculating Euclidean distance distance = np.linalg.norm(A - B) print(f"Euclidean Distance: {distance}") # Euclidean Distance: 48.425200051213004
The main benefit of this approach is that we need to call one method for its entire calculation. It is optimized for higher-dimensional vectors.
It can calculate other types of vector norms (e.g., L1 norm, infinity norm) by changing the “ord” parameter. I highly recommend using this method for most situations.
Method 2: Using np.sqrt() and np.sum()
This approach calculates Euclidean Distance step-by-step using NumPy’s sqrt() and sum() functions.
Step-by-step guide
- Calculate element-wise subtraction between two arrays.
- Square each element of the resulting array.
- Sum all the squared elements.
- Finally, calculate the square root of the resulting sum.
import numpy as np # Defining two points in space A = np.array([19, 21, 30]) B = np.array([41, 51, 61]) # Calculating Euclidean distance using np.sqrt() and np.sum() distance = np.sqrt(np.sum((A - B) ** 2)) print(f"Euclidean Distance: {distance}") # Euclidean Distance: 48.425200051213004
This approach is transparent and reflects the mathematical formula but also looks more verbose.
It is less efficient than np.linalg.norm() method. However, you can use this approach when a performance is not critical.
Method 3: Using np.dot()
The np.dot() method leverages the mathematical property that the dot product of a vector with itself is equal to the sum of the squares of its components.
import numpy as np # Defining two points in space A = np.array([19, 21, 30]) B = np.array([41, 51, 61]) # Calculating Euclidean distance using np.sqrt() and np.sum() euclidean_distance = np.sqrt(np.dot(A - B, A - B)) print(f"Euclidean Distance: {euclidean_distance}") # Euclidean Distance: 48.425200051213004
If your code already uses dot product calculations, you can use this method because it fits in naturally.
That’s it!