Honestly there's probably not going to be anything faster than np.inner or np.dot. If you find intermediate variables annoying, you could always create a lambda function:

sqeuclidean = lambda x: np.inner(x, x)

np.inner and np.dot leverage BLAS routines, and will almost certainly be faster than standard elementwise multiplication followed by summation.

In [1]: %%timeit -n 1 -r 100 a, b = np.random.randn(2, 1000000)
((a - b) ** 2).sum()
   ....: 
The slowest run took 36.13 times longer than the fastest. This could mean that an intermediate result is being cached 
1 loops, best of 100: 6.45 ms per loop

In [2]: %%timeit -n 1 -r 100 a, b = np.random.randn(2, 1000000)                                                                                                                                                                              
np.linalg.norm(a - b, ord=2) ** 2
   ....: 
1 loops, best of 100: 2.74 ms per loop

In [3]: %%timeit -n 1 -r 100 a, b = np.random.randn(2, 1000000)
sqeuclidean(a - b)
   ....: 
1 loops, best of 100: 2.64 ms per loop

np.linalg.norm(..., ord=2) uses np.dot internally, and gives very similar performance to using np.inner directly.

Answer from ali_m on Stack Overflow
🌐
DataCamp
datacamp.com › doc › numpy › linalg-norm
NumPy linalg.norm()
import numpy as np vector = np.array([3, 4]) norm = np.linalg.norm(vector) print(norm) This example computes the L2 norm (Euclidean norm) of a 2D vector, which results in 5.0, representing its magnitude.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.linalg.norm.html
numpy.linalg.norm — NumPy v2.4 Manual
Input array. If axis is None, x must be 1-D or 2-D, unless ord is None. If both axis and ord are None, the 2-norm of x.ravel will be returned.
Discussions

python - NumPy calculate square of norm 2 of vector - Stack Overflow
I have vector a. I want to calculate np.inner(a, a) But I wonder whether there is prettier way to calc it. [The disadvantage of this way, that if I want to calculate it for a-b or a bit more complex More on stackoverflow.com
🌐 stackoverflow.com
python - How to apply numpy.linalg.norm to each row of a matrix? - Stack Overflow
Note that, as perimosocordiae shows, as of NumPy version 1.9, np.linalg.norm(x, axis=1) is the fastest way to compute the L2-norm. More on stackoverflow.com
🌐 stackoverflow.com
How to make numba work with numpy’s linalg.norm function?
Curiosity got the better of me, so I took u/vlovero 's function and slapped njit on it. Here are the results on a 3700X: Test function: @nb.njit def frobenius_norm(a): norms = np.empty(a.shape[1], dtype=a.dtype) for i in nb.prange(a.shape[1]): norms[i] = np.sqrt(a[0, i] * a[0, i] + a[1, i] * a[1, i]) return norms Test data: np.random.randn(4, 10_000_000) # f64 np.random.randn(4, 10_000_000).astype(np.float32) # f32 Version | Time (f64) | Time (f32) | np.linalg.norm | 80.3 ms ± 1.39 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) | 38.1 ms ± 337 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) | @njit | 18.2 ms ± 208 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) | 7.21 ms ± 858 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) | @njit(parallel=True) | 13.8 ms ± 208 µs per loop (mean ± std. dev. of 7 runs, 1 loop each) | 5.7 ms ± 19.6 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) Inspecting the assembly shows decent instruction selection as well: .LBB3_5: vmovupd (%rbp,%r15,8), %ymm0 vmovupd (%r14,%r15,8), %ymm1 vmulpd %ymm0, %ymm0, %ymm0 vfmadd213pd %ymm0, %ymm1, %ymm1 vsqrtpd %ymm1, %ymm0 vmovupd %ymm0, (%r10,%r15,8) addq $4, %r15 cmpq %r15, %rdx jne .LBB3_5 More on reddit.com
🌐 r/learnpython
20
1
July 1, 2020
Is there a vectorized/faster version of numpy’s linalg.norm, or is that function already vectorized?
I guess you could calculate the norm on your own and get rid of square root. And of course then you have to square the element you're comparing to. With your code it should be something like: newArray = newArray[: , ((newArray[0:2,:]**2).sum(axis=0) < dia**2/4] It might be even 2x faster. More on reddit.com
🌐 r/learnpython
2
1
January 1, 2020
🌐
OpenGenus
iq.opengenus.org › norm-method-of-numpy-in-python
.norm() method of Numpy library in Python
June 3, 2020 - from numpy import array from numpy.linalg import norm v = array([1,2,3]) l1 = norm(v,1) print(l1) ... This one is also known as "Euclidian Norm", represented as ||V||2, where V is the representation for the vector.
🌐
Educative
educative.io › answers › what-is-the-nplinalgnorm-method-in-numpy
What is the np.linalg.norm() method in NumPy?
Let’s see different code examples of the numpy.linalg.norm() function with different parameter configurations. ... Line 6: it first creates a 3x3 matrix filled with numbers from -3 to 5. Line 13: it calculates the Frobenius norm of the matrix using LNG.norm(a), which computes the square root of the sum of the squared values of all elements. Line 16: it calculates the vector norms along the columns of the matrix using LNG.norm(a, axis=0), which computes the Euclidean/L2 norm of each column vector.
🌐
Analytics Vidhya
analyticsvidhya.com › home › exploring the power of norms with numpy linalg
The Power of Norms with NumPy Linalg | Analytics Vidhya
May 28, 2025 - NumPy, a popular library for numerical computing in Python, provides a comprehensive set of functions for linear algebra operations. One of the essential functions in NumPy’s linear algebra module, linalg, is the norm function. The linalg.norm function allows us to calculate vector and matrix norms efficiently. NumPy supports various norms, each with its characteristics and applications. Let’s explore some of the most commonly used norms in NumPy: The Euclidean norm, or the L2 norm, is perhaps the most well-known norm.
🌐
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 calculate the L1 and L2 norms of a vector or the Frobenius norm of a matrix in NumPy with np.linalg.norm(). This post explains the API and gives a few concrete usage examples.
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › linalg › norm
Python Numpy linalg norm() - Calculate Vector Norm | Vultr Docs
November 18, 2024 - Import the NumPy library. Define a vector for which the norm is to be calculated. Use the norm() function to compute the Euclidean norm. ... This code snippet calculates the Euclidean norm (also known as L2 norm) for the vector [3, 4]. The result ...
Find elsewhere
🌐
Programiz
programiz.com › python-programming › numpy › methods › norm
NumPy norm() (With Examples)
import numpy as np # create a matrix ... along columns: [ 6. 15. 24.] Here, the L2 norm along each row is calculated by taking the square root of the sum of the squared absolute values of the elements in each row....
🌐
Medium
medium.com › @heyamit10 › numpy-norm-fdc8dc604183
What is numpy.linalg.norm and Why is it Useful? | by Hey Amit | Medium
April 18, 2025 - Great question! By default, the ord parameter is set to 2. This means numpy.linalg.norm calculates the L2 norm, which is the Euclidean distance.
🌐
KDnuggets
kdnuggets.com › 2023 › 05 › vector-matrix-norms-numpy-linalg-norm.html
Vector and Matrix Norms with NumPy Linalg Norm - KDnuggets
We generally do not compute L1 and L2 norms on matrices, but NumPy lets you compute norms of any ord on matrices (2D-arrays) and other multi-dimensional arrays.
🌐
DigitalOcean
digitalocean.com › community › tutorials › norm-of-vector-python
Norm of a Vector in Python - Steps for Calculation | DigitalOcean
August 3, 2022 - L2 norm is always a positive quantity since we are squaring the values before adding them. The Python implementation is as follows : from numpy import array from numpy.linalg import norm arr = array([1, 2, 3, 4, 5]) print(arr) norm_l2 = norm(arr) print(norm_l2) Output : [1 2 3 4 5] ...
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.linalg.norm.html
numpy.linalg.norm — NumPy v2.5.dev0 Manual
Input array. If axis is None, x must be 1-D or 2-D, unless ord is None. If both axis and ord are None, the 2-norm of x.ravel will be returned.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.linalg.norm.html
numpy.linalg.norm — NumPy v2.1 Manual
Input array. If axis is None, x must be 1-D or 2-D, unless ord is None. If both axis and ord are None, the 2-norm of x.ravel will be returned.
Top answer
1 of 5
103

For numpy 1.9+

Note that, as perimosocordiae shows, as of NumPy version 1.9, np.linalg.norm(x, axis=1) is the fastest way to compute the L2-norm.

For numpy < 1.9

If you are computing an L2-norm, you could compute it directly (using the axis=-1 argument to sum along rows):

np.sum(np.abs(x)**2,axis=-1)**(1./2)

Lp-norms can be computed similarly of course.

It is considerably faster than np.apply_along_axis, though perhaps not as convenient:

In [48]: %timeit np.apply_along_axis(np.linalg.norm, 1, x)
1000 loops, best of 3: 208 us per loop

In [49]: %timeit np.sum(np.abs(x)**2,axis=-1)**(1./2)
100000 loops, best of 3: 18.3 us per loop

Other ord forms of norm can be computed directly too (with similar speedups):

In [55]: %timeit np.apply_along_axis(lambda row:np.linalg.norm(row,ord=1), 1, x)
1000 loops, best of 3: 203 us per loop

In [54]: %timeit np.sum(abs(x), axis=-1)
100000 loops, best of 3: 10.9 us per loop
2 of 5
61

Resurrecting an old question due to a numpy update. As of the 1.9 release, numpy.linalg.norm now accepts an axis argument. [code, documentation]

This is the new fastest method in town:

In [10]: x = np.random.random((500,500))

In [11]: %timeit np.apply_along_axis(np.linalg.norm, 1, x)
10 loops, best of 3: 21 ms per loop

In [12]: %timeit np.sum(np.abs(x)**2,axis=-1)**(1./2)
100 loops, best of 3: 2.6 ms per loop

In [13]: %timeit np.linalg.norm(x, axis=1)
1000 loops, best of 3: 1.4 ms per loop

And to prove it's calculating the same thing:

In [14]: np.allclose(np.linalg.norm(x, axis=1), np.sum(np.abs(x)**2,axis=-1)**(1./2))
Out[14]: True
🌐
AskPython
askpython.com › home › how to compute l1 and l2 norms in python?
How to compute L1 and L2 norms in python? - AskPython
February 27, 2023 - In this tutorial, we covered the basics of the L1 and L2 norms and the different terminologies associated with them. We also learned how to compute the norms using the numpy library in python. The np.linalg module in numpy provides several functions for linear algebra computations, including ...
🌐
Educative
educative.io › answers › l2-norm-in-python
L2 norm in Python
Lines 3 and 4: To store the heights of three people we created two Numpy arrays called actual_value and predicted_value. The predicted_value contains the heights predicted by a machine learning model. Line 7: We calculate the differences between the actual_value and predicted_value arrays. We used the np.power to square the differences between the elements of two arrays. We use np.sum to sum the square resulting values. Line 10: Finally, we take the square root of the l2_norm using np.sqrt this value shows the difference between the predicted values and actual value.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.linalg.vector_norm.html
numpy.linalg.vector_norm — NumPy v2.5.dev0 Manual
If an integer, axis specifies the axis (dimension) along which to compute vector norms. If an n-tuple, axis specifies the axes (dimensions) along which to compute batched vector norms. If None, the vector norm must be computed over all array values (i.e., equivalent to computing the vector ...
🌐
Medium
koshurai.medium.com › demystifying-l1-norm-and-l2-norm-in-python-your-guide-to-understanding-and-implementing-6390ee0ae8fe
Demystifying L1 Norm and L2 Norm in Python: Your Guide to Understanding and Implementing | by KoshurAI | Medium
February 25, 2024 - In this code snippet, we use NumPy’s linalg.norm() function to compute the norms of our vector. The ord parameter specifies the type of norm we want to calculate: ord=1 for L1 norm and ord=2 for L2 norm.