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
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.linalg.norm.html
numpy.linalg.norm — NumPy v2.4 Manual
Both the Frobenius and nuclear norm orders are only defined for matrices and raise a ValueError when x.ndim != 2. ... G. H. Golub and C. F. Van Loan, Matrix Computations, Baltimore, MD, Johns Hopkins University Press, 1985, pg. 15 ... Try it in your browser! >>> import numpy as np >>> from numpy import linalg as LA >>> a = np.arange(9) - 4 >>> a array([-4, -3, -2, ..., 2, 3, 4]) >>> b = a.reshape((3, 3)) >>> b array([[-4, -3, -2], [-1, 0, 1], [ 2, 3, 4]])
🌐
GeeksforGeeks
geeksforgeeks.org › calculate-the-euclidean-distance-using-numpy
Calculate the Euclidean distance using NumPy - GeeksforGeeks
April 29, 2025 - Let's discuss a few ways to find Euclidean distance by NumPy library. 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.
🌐
W3docs
w3docs.com › python
How can the Euclidean distance be calculated with NumPy?
import numpy as np # define two points point1 = np.array([1, 2, 3]) point2 = np.array([4, 5, 6]) # calculate Euclidean distance distance = np.linalg.norm(point1 - point2) print(distance)
🌐
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 - The first option we have when it comes to computing Euclidean distance is [numpy.linalg.norm()](https://numpy.org/doc/stable/reference/generated/numpy.linalg.norm.html) function, that is used to return one of eight different matrix norms.
🌐
Medium
medium.com › @whyamit101 › understanding-euclidean-distance-with-numpy-3fe9949ff196
Understanding Euclidean Distance with Numpy | by why amit | Medium
February 9, 2025 - import numpy as np # Define two points point1 = np.array([1, 2, 3]) point2 = np.array([4, 5, 6]) # Compute the Euclidean distance distance = np.linalg.norm(point1 - point2) print("Euclidean Distance:", distance)
🌐
Stack Abuse
stackabuse.com › calculating-euclidean-distance-with-numpy
Calculating Euclidean Distance with NumPy
October 17, 2023 - Euclidean distance is the L2 norm of a vector (sometimes known as the Euclidean norm) and by default, the norm() function uses L2 - the ord parameter is set to 2. If you were to set the ord parameter to some other value p, you'd calculate other p-norms. For instance, the L1 norm of a vector ...
Find elsewhere
🌐
Medium
medium.com › @heyamit10 › numpy-norm-fdc8dc604183
What is numpy.linalg.norm and Why is it Useful? | by Hey Amit | Medium
April 18, 2025 - By default, the ord parameter is set to 2. This means numpy.linalg.norm calculates the L2 norm, which is the Euclidean distance. Think of it as the straight-line distance in multi-dimensional space.
Top answer
1 of 2
43

A norm is a function that takes a vector as an input and returns a scalar value that can be interpreted as the "size", "length" or "magnitude" of that vector. More formally, norms are defined as having the following mathematical properties:

  • They scale multiplicatively, i.e. Norm(a·v) = |a|·Norm(v) for any scalar a
  • They satisfy the triangle inequality, i.e. Norm(u + v) ≤ Norm(u) + Norm(v)
  • The norm of a vector is zero if and only if it is the zero vector, i.e. Norm(v) = 0 ⇔ v = 0

The Euclidean norm (also known as the L² norm) is just one of many different norms - there is also the max norm, the Manhattan norm etc. The L² norm of a single vector is equivalent to the Euclidean distance from that point to the origin, and the L² norm of the difference between two vectors is equivalent to the Euclidean distance between the two points.


As @nobar's answer says, np.linalg.norm(x - y, ord=2) (or just np.linalg.norm(x - y)) will give you Euclidean distance between the vectors x and y.

Since you want to compute the Euclidean distance between a[1, :] and every other row in a, you could do this a lot faster by eliminating the for loop and broadcasting over the rows of a:

dist = np.linalg.norm(a[1:2] - a, axis=1)

It's also easy to compute the Euclidean distance yourself using broadcasting:

dist = np.sqrt(((a[1:2] - a) ** 2).sum(1))

The fastest method is probably scipy.spatial.distance.cdist:

from scipy.spatial.distance import cdist

dist = cdist(a[1:2], a)[0]

Some timings for a (1000, 1000) array:

a = np.random.randn(1000, 1000)

%timeit np.linalg.norm(a[1:2] - a, axis=1)
# 100 loops, best of 3: 5.43 ms per loop

%timeit np.sqrt(((a[1:2] - a) ** 2).sum(1))
# 100 loops, best of 3: 5.5 ms per loop

%timeit cdist(a[1:2], a)[0]
# 1000 loops, best of 3: 1.38 ms per loop

# check that all 3 methods return the same result
d1 = np.linalg.norm(a[1:2] - a, axis=1)
d2 = np.sqrt(((a[1:2] - a) ** 2).sum(1))
d3 = cdist(a[1:2], a)[0]

assert np.allclose(d1, d2) and np.allclose(d1, d3)
2 of 2
5

The concept of a "norm" is a generalized idea in mathematics which, when applied to vectors (or vector differences), broadly represents some measure of length. There are various different approaches to computing a norm, but the one called Euclidean distance is called the "2-norm" and is based on applying an exponent of 2 (the "square"), and after summing applying an exponent of 1/2 (the "square root").


It's a bit cryptic in the docs, but you get Euclidean distance between two vectors by setting the parameter ord=2.

sum(abs(x)**ord)**(1./ord)

becomes sqrt(sum(x**2)).

Note: as pointed out by @Holt, the default value is ord=None, which is documented to compute the "2-norm" for vectors. This is, therefore, equivalent to ord=2 (Euclidean distance).

🌐
Kodeclik
kodeclik.com › euclidean-distance-python-numpy
Calculate Euclidean distance in Python numpy
October 16, 2024 - The final approach to compute the Euclidean distance is the easiest and it uses the built-in function called linalg.norm() within the numpy module. Here is how that works: import numpy as np location1 = np.array([-1,1]) location2 = np.array([2,-3]) print("Location 1's coordinates:") print(location1) print("Location 2's coordinates:") print(location2) print("Distance between location 1 and location 2:") print(np.linalg.norm(location1-location2))
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.linalg.norm.html
numpy.linalg.norm — NumPy v2.1 Manual
Both the Frobenius and nuclear norm orders are only defined for matrices and raise a ValueError when x.ndim != 2. ... G. H. Golub and C. F. Van Loan, Matrix Computations, Baltimore, MD, Johns Hopkins University Press, 1985, pg. 15 ... >>> import numpy as np >>> from numpy import linalg as LA >>> a = np.arange(9) - 4 >>> a array([-4, -3, -2, ..., 2, 3, 4]) >>> b = a.reshape((3, 3)) >>> b array([[-4, -3, -2], [-1, 0, 1], [ 2, 3, 4]])
🌐
GitHub
github.com › dask › dask › issues › 5290
Euclidean distance calculation in dask.array.linalg.norm slower than in numpy.linalg.norm · Issue #5290 · dask/dask
August 17, 2019 - Solution with SciPy") t1_np = time() # Calculate the euclidean distances from point XA to all points in the XB array np_dist_1 = cdist(XA=xa, XB=xb, metric="euclidean") print(f"\n1.1. Time exec of scipy.spatial.distance.cdist: {time()-t1_np:.3f} sec") t3_np = time() # Let's find TOP_N the indexes of the smallest distances np_top_dist_indexes = np_dist_1.argsort()[0][:TOP_N].copy() # Get an array of distances by their indexes np_top_dist_values = np_dist_1[0][np_top_dist_indexes] print(f"Time exec of TOP_N: {time()-t3_np:.3f} sec") pprint(np_top_dist_indexes) pprint(np_top_dist_values) t2_np = time() np_dist_2 = np.array([numpy.linalg.norm(xb-xa, axis=1)]) print(f"\n1.2.
Author   manmitya
🌐
Statology
statology.org › home › how to calculate euclidean distance in python (with examples)
How to Calculate Euclidean Distance in Python (With Examples)
October 18, 2020 - To calculate the Euclidean distance between two vectors in Python, we can use the numpy.linalg.norm function: #import functions import numpy as np from numpy.linalg import norm #define two vectors a = np.array([2, 6, 7, 7, 5, 13, 14, 17, 11, 8]) b = np.array([3, 5, 5, 3, 7, 12, 13, 19, 22, 7]) #calculate Euclidean distance between the two vectors norm(a-b) 12.409673645990857 ·
🌐
Sparrow Computing
sparrow.dev › home › blog › numpy norm: understanding np.linalg.norm()
NumPy Norm: Understanding np.linalg.norm() - Sparrow Computing
October 15, 2021 - You can also use np.linalg.norm() to compute pairwise Euclidean distance between two sets of points.
🌐
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
Euclidean distance is defined in mathematics as the magnitude or length of the line segment between two points. ... In this method, we first initialize two numpy arrays. Then, we use linalg.norm() of numpy to compute the Euclidean distance directly.
🌐
CliffsNotes
cliffsnotes.com › questions & answers › artificial intelligence › what does numpy.linalg.norm do for the following values? i'm at a loss for it. train_images = [[0.13333333 0.15294118...
[Solved] What does numpy.linalg.norm do for the following values? I'm at a loss for it. train_images = [[0.13333333 0.15294118... | CliffsNotes
February 12, 2023 - 0.12941176 0.19215686 0.23529412] [0.38823529 0.38431373 0.38431373 ... 0.57647059 0.58039216 0.56862745] [0.79607843 0.79215686 0.79607843 ... 0.77647059 0.77647059 0.76862745]...] #A 2D array of 4096D RGB values #linalg = numpy.linalg.norm(train_images) linalg = [24.82338754 29.76918332 ...
🌐
ProjectPro
projectpro.io › recipes › compute-euclidean-distance-between-two-arrays
How to compute the euclidean distance between two arrays in numpy -
May 11, 2022 - Euclidean_distance = np.linalg.norm(data_pointA - data_pointB) print("The Euclidean distance between two points are:", Euclidean_distance) The Euclidean distance between two points are: 5.196152422706632 · Graduate Research assistance at Stony Brook University ·
🌐
Quora
quora.com › How-should-I-calculate-the-Euclidean-distance-of-two-NumPy-Nd-array-of-shape-30-4-and-120-4
How should I calculate the Euclidean distance of two NumPy Nd-array of shape (30,4) and (120,4)? - Quora
Those two objects have different dimensionality. The Euclidean distance is defined for vectors. For matrices there is the Frobenius norm, which is very similar to the Euclidean norm for vectors.