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
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › linalg › norm
Python Numpy linalg norm() - Calculate Vector Norm | Vultr Docs
November 18, 2024 - 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 is 5.0, which is the straight-line distance ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › norm-of-vector-python
Norm of a Vector in Python - Steps for Calculation | DigitalOcean
August 3, 2022 - Let’s consider an example to understand it. ... L2 norm is always a positive quantity since we are squaring the values before adding them. ... 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)
🌐
DataCamp
datacamp.com › doc › numpy › linalg-norm
NumPy linalg.norm()
This example computes the L2 norm ... print(norm) Here, the Frobenius norm is specified using the `ord` parameter. It calculates the square root of the sum of the absolute squares of its elements....
🌐
The Security Buddy
thesecuritybuddy.com › home › linear algebra › how to calculate the l2 norm of a vector using python?
How to calculate the L2 norm of a vector using Python? - The Security Buddy
October 3, 2023 - import numpy from numpy.linalg import norm v = numpy.array([1, 2, -3]) l2 = norm(v) print("Vector v: \n", v) print("L1 norm of the vector v: ", l2) Here, v is a vector. We are using the norm() function from numpy.linalg to calculate the L2 norm ...
🌐
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 - On the other hand, the L2 norm, also known as the Euclidean norm, calculates the square root of the sum of the squared values of the vector components. Think of it as the straight-line distance between two points in Euclidean space — the kind ...
🌐
Programiz
programiz.com › python-programming › numpy › methods › norm
NumPy norm() (With Examples)
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.
🌐
Codecademy
codecademy.com › docs › python:numpy › linear algebra › .norm()
Python:NumPy | Linear Algebra | .norm() | Codecademy
May 26, 2025 - Returns a float (for a single norm) or an array of floats (when computing norms along specific axes). This example demonstrates how to calculate the default 2-norm (Euclidean norm) of a vector:
Find elsewhere
🌐
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.
🌐
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 the computation of vector norms. By using the norm function in np.linalg, we can easily calculate the L1 or L2 norm of a given vector.
🌐
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.
🌐
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.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.linalg.norm.html
numpy.linalg.norm — NumPy v2.2 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.
🌐
w3resource
w3resource.com › python-exercises › numpy › linear-algebra › numpy-linear-algebra-exercise-10.php
NumPy: Find a matrix or vector norm - w3resource
May 5, 2025 - Write a NumPy program to find a matrix or vector norm. ... # Import the NumPy library and alias it as 'np' import numpy as np # Create a NumPy array 'v' containing elements from 0 to 6 v = np.arange(7) # Calculate the L2 norm (Euclidean norm) of the vector 'v' result = np.linalg.norm(v) # Display the computed L2 norm of the vector 'v' print("Vector norm:") print(result) # Create a 2x2 matrix 'm' using the np.matrix function m = np.matrix('1, 2; 3, 4') # Calculate the Frobenius norm of the matrix 'm' result1 = np.linalg.norm(m) # Display the computed Frobenius norm of the matrix 'm' print("Matrix norm:") print(result1)
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
🌐
KDnuggets
kdnuggets.com › 2023 › 05 › vector-matrix-norms-numpy-linalg-norm.html
Vector and Matrix Norms with NumPy Linalg Norm - KDnuggets
Substituting p =2 in the general Lp norm equation, we get the following expression for the L2 norm of a vector: For a given vector x, the L∞ norm is the maximum of the absolute values of the elements of x: It’s fairly straightforward to verify that all of these norms satisfy the properties of norms listed earlier. The linalg module in NumPy has functions that we can use to compute norms.
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.linalg.norm.html
numpy.linalg.norm — NumPy v2.3 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.
🌐
Educative
educative.io › answers › l2-norm-in-python
L2 norm in Python
Lines 3 and 4: To store the heights ... 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 ...