Calculate the Euclidean distance using NumPy
Last Updated :
15 Jul, 2025
Euclidean distance is the shortest between the 2 points irrespective of the dimensions. In this article to find the Euclidean distance, we will use the NumPy library. This library used for manipulating multidimensional array in a very efficient way. Let's discuss a few ways to find Euclidean distance by NumPy library.
Using np.linalg.norm()
np.linalg.norm() function computes the norm (or magnitude) of a vector, which in the case of the difference between two points, gives us the Euclidean distance. It's a simple and efficient way to find the distance.
Python
import numpy as np
p1 = np.array((1, 2, 3))
p2 = np.array((1, 1, 1))
d = np.linalg.norm(p1 - p2)
print(d)
Explanation: We subtract p2 from p1 to get the difference vector (0, 1, 2). Then, np.linalg.norm(p1 - p2) directly calculates the Euclidean distance by finding the magnitude of the difference vector.
Using **
Here, we're manually calculating the Euclidean distance by summing the squares of the differences between corresponding coordinates and then taking the square root of that sum. It's a more "hands-on" approach compared to np.linalg.norm() but is functionally identical.
Python
import numpy as np
p1 = np.array((1, 2, 3))
p2 = np.array((1, 1, 1))
d = np.sqrt(np.sum((p1 - p2)**2))
print(d)
Explanation: (p1 - p2)**2 squares each element of the difference vector. np.sum() adds up these squared values (0 + 1 + 4 = 5).
Using np.einsum()
np.einsum() function compute the dot product of the difference vector with itself, effectively squaring and summing the differences. While efficient for complex calculations, it may be overkill for simple distance computation.
Python
import numpy as np
p1 = np.array((1, 2, 3))
p2 = np.array((1, 1, 1))
d = np.sqrt(np.einsum('i,i->', p1 - p2, p1 - p2))
print(d)
Explanation: First, we subtract p2 from p1 to get the difference vector (0, 1, 2). Then, np.einsum('i,i->', p1 - p2, p1 - p2) calculates the dot product of this vector with itself, summing the squared differences (0 + 1 + 4 = 5).
Using np.dot()
np.dot() function computes the dot product of the difference vector with itself, effectively summing the squared differences. Applying np.sqrt() then gives the Euclidean distance between the two points.
Python
import numpy as np
p1 = np.array((1, 2, 3))
p2 = np.array((1, 1, 1))
d = np.sqrt(np.dot(p1 - p2, p1 - p2))
print(d)
Explanation: This code calculates the difference vector (0, 1, 2) and then uses np.dot() to compute the sum of squared differences. Finally, np.sqrt() is applied to this sum.
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. OOPs is a way of organizing code that uses objects and classes to represent real-world entities and their behavior. In OOPs, object has attributes thing th
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython's input() function
7 min read
Python Lists In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s
6 min read