Use numpy.linalg.norm:

dist = numpy.linalg.norm(a-b)

This works because the Euclidean distance is the l2 norm, and the default value of the ord parameter in numpy.linalg.norm is 2. For more theory, see Introduction to Data Mining:

Answer from u0b34a0f6ae on Stack Overflow
🌐
scikit-learn
scikit-learn.org › stable › modules › generated › sklearn.metrics.pairwise.euclidean_distances.html
euclidean_distances — scikit-learn 1.8.0 documentation
sklearn.metrics.pairwise.euclidean_distances(X, Y=None, *, Y_norm_squared=None, squared=False, X_norm_squared=None)[source]# Compute the distance matrix between each pair from a feature array X and Y.
Discussions

Computing Euclidean distance for numpy in python - Stack Overflow
I want to compute the euclidean distance between all pairs of nodes from this set and store them in a pairwise matrix. For example, If I have 20 nodes, I want the end result to be a matrix of (20,20) with values of euclidean distance between each pairs of nodes. I tried to used a for loop to go through each element of the coordinate set and compute euclidean distance as follows: ncoord=numpy... More on stackoverflow.com
🌐 stackoverflow.com
python - In Numpy, find Euclidean distance between each pair from two arrays - Stack Overflow
I have two arrays of 2D coordinate points (x,y) a = [ (x1,y1), (x2,y2), ... (xN,yN) ] b = [ (X1,Y1), (X2,Y2), ... (XN,YN) ] How can I find the Euclidean distances between each aligned pairs (xi,yi... More on stackoverflow.com
🌐 stackoverflow.com
python - How to calculate euclidean distance between pair of rows of a numpy array - Stack Overflow
I think it is giving me the euclidean distance between each pair of points but I want it between each pair of rows. Consider one row represents one 1d vector. More on stackoverflow.com
🌐 stackoverflow.com
April 12, 2017
How to calculate euclidean distance using NumPy? - Python - Data Science Dojo Discussions
I’m finding Euclidean distance using the NumPy array but it’s not working the way I want. import numpy as np vec = np.random.rand(5, 2) distances = np.zeros((5, 5)) for i in range(100): for j in range(i, 5): distances[i, j] = np.sqrt(((vec[i] - vec[j]) ** 2).sum()) distances[j, i] = ... More on discuss.datasciencedojo.com
🌐 discuss.datasciencedojo.com
1
0
March 1, 2023
🌐
SciPy
docs.scipy.org › doc › scipy › reference › generated › scipy.spatial.distance.cdist.html
cdist — SciPy v1.17.0 Manual
Instead, the optimized C version is more efficient, and we call it using the following syntax: ... Try it in your browser! Find the Euclidean distances between four 2-D coordinates: >>> from scipy.spatial import distance >>> import numpy as np >>> coords = [(35.0456, -85.2672), ...
🌐
Jay Mody
jaykmody.com › blog › distance-matrices-with-numpy
Computing Distance Matrices with NumPy | Jay Mody
April 4, 2021 - Most simple way to compute our distance matrix is to just loop over all the pairs and elements: X # test data (m, d) X_train # train data (n, d) m = X.shape[0] n = X_train.shape[0] d = X.shape[1] dists = np.zeros((num_test, num_train)) # distance ...
Top answer
1 of 4
29

There are much, much faster alternatives to using nested for loops for this. I'll show you two different approaches - the first will be a more general method that will introduce you to broadcasting and vectorization, and the second uses a more convenient scipy library function.


  1. The general way, using broadcasting & vectorization

One of the first things I'd suggest doing is switching to using np.array rather than np.matrix. Arrays are preferred for a number of reasons, most importantly because they can have >2 dimensions, and they make element-wise multiplication much less awkward.

import numpy as np

ncoord = np.array(ncoord)

With an array, we can eliminate the nested for loops by inserting a new singleton dimension and broadcasting the subtraction over it:

# indexing with None (or np.newaxis) inserts a new dimension of size 1
print(ncoord[:, :, None].shape)
# (20, 2, 1)

# by making the 'inner' dimensions equal to 1, i.e. (20, 2, 1) - (1, 2, 20),
# the subtraction is 'broadcast' over every pair of rows in ncoord
xydiff = ncoord[:, :, None] - ncoord[:, :, None].T

print(xydiff.shape)
# (20, 2, 20)

This is equivalent to looping over every pair of rows using nested for loops, but much, much faster!

xydiff2 = np.zeros((20, 2, 20), dtype=xydiff.dtype)
for ii in range(20):
    for jj in range(20):
        for kk in range(2):
            xydiff[ii, kk, jj] = ncoords[ii, kk] - ncoords[jj, kk]

# check that these give the same result
print(np.all(xydiff == xydiff2))
# True

The rest we can also do using vectorized operations:

# we square the differences and sum over the 'middle' axis, equivalent to
# computing (x_i - x_j) ** 2 + (y_i - y_j) ** 2
ssdiff = (xydiff * xydiff).sum(1)

# finally we take the square root
D = np.sqrt(ssdiff)

The whole thing could be done in one line like this:

D = np.sqrt(((ncoord[:, :, None] - ncoord[:, :, None].T) ** 2).sum(1))

  1. The lazy way, using pdist

It turns out that there's already a fast and convenient function for computing all pairwise distances: scipy.spatial.distance.pdist.

from scipy.spatial.distance import pdist, squareform

d = pdist(ncoord)

# pdist just returns the upper triangle of the pairwise distance matrix. to get
# the whole (20, 20) array we can use squareform:

print(d.shape)
# (190,)

D2 = squareform(d)
print(D2.shape)
# (20, 20)

# check that the two methods are equivalent
print np.all(D == D2)
# True
2 of 4
5
for i in range(0, n):
    for j in range(i+1, n):
        c[i, j] = math.sqrt((ncoord[i, 0] - ncoord[j, 0])**2 
        + (ncoord[i, 1] - ncoord[j, 1])**2)

Note: ncoord[i, j] is not the same as ncoord[i][j] for a Numpy matrix. This appears to be the source of confusion. If ncoord is a Numpy array then they will give the same result.

For a Numpy matrix, ncoord[i] returns the ith row of ncoord, which itself is a Numpy matrix object with shape 1 x 2 in your case. Therefore, ncoord[i][j] actually means: take the ith row of ncoord and take the jth row of that 1 x 2 matrix. This is where your indexing problems comes about when j > 0.

Regarding your comments on assigning to c[i][j] "working", it shouldn't. At least on my build of Numpy 1.9.1 it shouldn't work if your indices i and j iterates up to n.

As an aside, remember to add the transpose of the matrix c to itself.

It is recommended to use Numpy arrays instead of matrix. See this post.

If your coordinates are stored as a Numpy array, then pairwise distance can be computed as:

from scipy.spatial.distance import pdist

pairwise_distances = pdist(ncoord, metric="euclidean", p=2)

or simply

pairwise_distances = pdist(ncoord)

since the default metric is "euclidean", and default "p" is 2.

In a comment below I mistakenly mentioned that the result of pdist is a n x n matrix. To get a n x n matrix, you will need to do the following:

from scipy.spatial.distance import pdist, squareform

pairwise_distances = squareform(pdist(ncoord))

or

from scipy.spatial.distance import cdist

pairwise_distances = cdist(ncoord, ncoord)
🌐
Sparrow Computing
sparrow.dev › home › blog › pairwise distance in numpy
Pairwise Distance in NumPy - Sparrow Computing
October 15, 2021 - Then, we apply the L2 norm along the -1th axis (which is shorthand for the last axis). This gives us the Euclidean distance between each pair of points. There are a few benefits to using the NumPy approach over the SciPy approach.
🌐
Educative
educative.io › answers › how-to-compute-the-euclidean-distance-between-two-arrays-in-numpy
How to compute the Euclidean distance between two arrays in numpy
The details of the function can be found here. ... In this method, we first initialize two numpy arrays. Then, we take the difference of the two arrays, compute the dot product of the result, and transpose of the result.
Find elsewhere
🌐
Towards Data Science
towardsdatascience.com › home › latest › how to compute euclidean distance in numpy
How To Compute Euclidean Distance in NumPy | Towards Data Science
January 29, 2025 - Therefore, in order to compute the Euclidean Distance we can simply pass the difference of the two NumPy arrays to this function: ... The Scipy package offers a module with numerous functions that compute various types of distance metrics, including Euclidean Distance. More specifically, the scipy.spatial.distance.euclidean function can compute the Euclidean Distance between two 1-D arrays.
🌐
Nenadmarkus
nenadmarkus.com › p › all-pairs-euclidean
All-pairs Euclidean distance
May 10, 2019 - where $(\mathbf{D})_{ij}$ is the ... vectors in $\mathbf{A}$ and all vectors in $\mathbf{B}$. The following numpy code does exactly this: def all_pairs_euclid_naive(A, B): # D = numpy.zeros((A.shape[0], B.shape[0]), dtype=numpy.float32) for i in range(0, D.shape[0]): for j ...
🌐
GeeksforGeeks
geeksforgeeks.org › calculate-the-euclidean-distance-using-numpy
Calculate the Euclidean distance using NumPy - GeeksforGeeks
April 29, 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.
🌐
Stack Abuse
stackabuse.com › calculating-euclidean-distance-with-numpy
Calculating Euclidean Distance with NumPy
October 17, 2023 - With NumPy, we can use the np.dot() function, passing in two vectors. If we calculate a Dot Product of the difference between both points, with that same difference - we get a number that's in a relationship with the Euclidean Distance between those two vectors.
🌐
Jejjohnson
jejjohnson.github.io › research_journal › snippets › numpy › euclidean
Efficient Euclidean Distance Calculation - Numpy Einsum - Research Journal
import numpy as np def euclidean_distance_einsum(X, Y): """Efficiently calculates the euclidean distance between two vectors using Numpys einsum function.
🌐
GitHub
github.com › eth-cscs › PythonHPC › blob › master › numpy › 03-euclidean-distance-matrix-numpy.ipynb
PythonHPC/numpy/03-euclidean-distance-matrix-numpy.ipynb at master · eth-cscs/PythonHPC
"In this notebook we implement two functions to compute the Euclidean distance matrix. We use a simple algebra trick that makes possible to write the function in a completely vectorized way in terms of optimized NumPy functions."
Author   eth-cscs
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
How to calculate euclidean distance using NumPy? - Python - Data Science Dojo Discussions
March 1, 2023 - I’m finding Euclidean distance ... a random and zeros array of shapes (5,2). Then, it uses a nested for loop to calculate the distances between all pairs of points....
🌐
Super Fast Python
superfastpython.com › numpy-parallel-vector-distance
Numpy Parallel Vector Distance Calculation – SuperFastPython
May 29, 2023 - Running the example calculates the Euclidean distance between all pairs of vectors and collects the distances in a list.
🌐
Shiksha
shiksha.com › home › data science › data science articles › machine learning articles › how to compute euclidean distance in python
How to Compute Euclidean Distance in Python - Shiksha Online
August 16, 2024 - How can I compute the Euclidean distance between multiple pairs of points in a dataset using Pandas? Pandas can be used in combination with NumPy to compute the Euclidean distance for multiple pairs of points in a DataFrame.
🌐
Medium
medium.com › @whyamit101 › understanding-euclidean-distance-with-numpy-3fe9949ff196
Understanding Euclidean Distance with Numpy | by why amit | Medium
February 9, 2025 - Define Points: You define your points as Numpy arrays. The key here is that each point is just a list of coordinates (like a GPS location in 3D or 2D space). Compute the Euclidean Distance: You use np.linalg.norm() to calculate the Euclidean distance, which essentially applies the formula we talked about earlier. Here’s the code for calculating the Euclidean distance between two points: