Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Calculate the Euclidean Distance using NumPy

  • 15 Jan, 2025
  • Com 0
Calculating the Euclidean Distance using NumPy

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:

  1. Using np.linalg.norm()
  2. Using np.sqrt() and np.sum()
  3. 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.

Method 1 - Using np.linalg.norm

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

  1. Calculate element-wise subtraction between two arrays.
  2. Square each element of the resulting array.
  3. Sum all the squared elements.
  4. 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.

Method 3 - Using np.dot()

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!

Post Views: 27
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Unzipping a List of Tuples in Python
How to Reverse Numpy Array (1D and Multi-Dimensional)

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend