Calculate the Euclidean distance using NumPy
Last Updated :
29 Apr, 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
Calculate Euclidean Distance Using Python OSMnx Distance Module Euclidean space is defined as the line segment length between two points. The distance can be calculated using the coordinate points and the Pythagoras theorem. In this article, we will see how to calculate Euclidean distances between Points Using the OSMnx distance module. Syntax of osmnx.distance.
4 min read
Python | Calculate Distance between two places using Geopy GeoPy is a Python library that makes geographical calculations easier for the users. In this article, we will see how to calculate the distance between 2 points on the earth in two ways. How to Install GeoPy ? pip install geopy Geodesic Distance: It is the length of the shortest path between 2 point
2 min read
How to Calculate the determinant of a matrix using NumPy? The determinant of a square matrix is a special number that helps determine whether the matrix is invertible and how it transforms space. It is widely used in linear algebra, geometry and solving equations. NumPy provides built-in functions to easily compute the determinant of a matrix, let's explor
2 min read
Pandas - Compute the Euclidean distance between two series There are many distance metrics that are used in various Machine Learning Algorithms. One of them is Euclidean Distance. Euclidean distance is the most used distance metric and it is simply a straight line distance between two points. Euclidean distance between points is given by the formula : \[d(x
2 min read
Python | Calculate City Block Distance City block distance is generally calculated between 2-coordinates of a paired object. It is the summation of absolute difference between 2-coordinates. The city block distance of 2-points a and b with k dimension is mathematically calculated using below formula: In this article two solution are expl
2 min read
Absolute Deviation and Absolute Mean Deviation using NumPy | Python Absolute value: Absolute value or the modulus of a real number x is the non-negative value of x without regard to its sign. For example absolute value of 7 is 7 and the absolute value of -7 is also 7. Deviation: Deviation is a measure of the difference between the observed value of a variable and so
3 min read
Calculate the sum of the diagonal elements of a NumPy array Sometimes we need to find the sum of the Upper right, Upper left, Lower right, or lower left diagonal elements. Numpy provides us the facility to compute the sum of different diagonals elements using numpy.trace() and numpy.diagonal() method. Method 1: Finding the sum of diagonal elements using nump
2 min read
Calculate Great Circle Distances Between Pairs of Points Using OSMnx Module The Great Circle Distance evaluates the shortest distance between two points considering Earth as a sphere. It is an arc linking two points on a sphere. Here, we will see how to calculate great circle distances between pairs of points using the OSMnx distance module. Syntax of osmnx.distance.great_c
3 min read
How to generate 2-D Gaussian array using NumPy? In this article, let us discuss how to generate a 2-D Gaussian array using NumPy. To create a 2 D Gaussian array using the Numpy python module. Functions used:numpy.meshgrid()- It is used to create a rectangular grid out of two given one-dimensional arrays representing the Cartesian indexing or Matr
2 min read
How to Calculate Cosine Similarity in Python? Cosine Similarity is a metric used to measure how similar two vectors are, regardless of their magnitude. It is frequently used in text analysis, recommendation systems, and clustering tasks, where the orientation of data (rather than its scale) is more important.The Cosine Similarity between two no
2 min read