I am not a mathematician but here is my layman's explanation of “norm”:

A vector describes the location of a point in space relative to the origin. Here’s an example in 2D space for the point [3 2]:

The norm is the distance from the origin to the point. In the 2D case it’s easy to visualize the point as the diametrically opposed point of a right triangle and see that the norm is the same thing as the hypotenuse.

However, In higher dimensions it’s no longer a shape we describe in average-person language, but the distance from the origin to the point is still called the norm. Here's an example in 3D space:

I don’t know why the norm is used in K-means clustering. You stated that it was part of determing the distance between the old and new centroid in each step. Not sure why one would use the norm for this since you can get the distance between two points in any dimensionality* using an extension of the from used in 2D algebra:

You just add a term for each addtional dimension, for example here is a 3D version:

*where the dimensions are positive integers

Answer from Robb Dunlap on Stack Overflow
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.linalg.norm.html
numpy.linalg.norm — NumPy v2.3 Manual
This function is able to return one of eight different matrix norms, or one of an infinite number of vector norms (described below), depending on the value of the ord parameter.
🌐
DataCamp
datacamp.com › doc › numpy › linalg-norm
NumPy linalg.norm()
The `numpy.linalg.norm()` function is used to compute different norms of vectors and matrices, which are essential in various mathematical and machine learning applications.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.linalg.norm.html
numpy.linalg.norm — NumPy v2.4 Manual
This function is able to return one of eight different matrix norms, or one of an infinite number of vector norms (described below), depending on the value of the ord parameter.
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › linalg › norm
Python Numpy linalg norm() - Calculate Vector Norm | Vultr Docs
November 18, 2024 - The norm() function from the numpy.linalg module is essential for calculating various types of norms for vectors and matrices. Whether it's the straightforward Euclidean norm, the summative Manhattan norm, or the maximum-seeking infinity 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.
🌐
Medium
medium.com › @heyamit10 › numpy-norm-fdc8dc604183
What is numpy.linalg.norm and Why is it Useful? | by Hey Amit | Medium
April 18, 2025 - So, what does it do? Simply put, numpy.linalg.norm helps you calculate the magnitude (or length) of a vector or the "size" of a matrix.
Top answer
1 of 3
10

I am not a mathematician but here is my layman's explanation of “norm”:

A vector describes the location of a point in space relative to the origin. Here’s an example in 2D space for the point [3 2]:

The norm is the distance from the origin to the point. In the 2D case it’s easy to visualize the point as the diametrically opposed point of a right triangle and see that the norm is the same thing as the hypotenuse.

However, In higher dimensions it’s no longer a shape we describe in average-person language, but the distance from the origin to the point is still called the norm. Here's an example in 3D space:

I don’t know why the norm is used in K-means clustering. You stated that it was part of determing the distance between the old and new centroid in each step. Not sure why one would use the norm for this since you can get the distance between two points in any dimensionality* using an extension of the from used in 2D algebra:

You just add a term for each addtional dimension, for example here is a 3D version:

*where the dimensions are positive integers

2 of 3
7

numpy.linalg.norm is used to calculate the norm of a vector or a matrix.


This is the help document taken from numpy.linalg.norm:

numpy.linalg.norm(x, ord=None, axis=None, keepdims=False)[source]


This is the code snippet taken from K-Means Clustering in Python:

# Euclidean Distance Caculator
def dist(a, b, ax=1):
    return np.linalg.norm(a - b, axis=ax)

It take order=None as default, so just to calculate the Frobenius norm of (a-b), this is ti calculate the distance between a and b( using the upper Formula).


Find elsewhere
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.linalg.matrix_norm.html
numpy.linalg.matrix_norm — NumPy v2.5.dev0 Manual
>>> 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]]) >>> LA.matrix_norm(b) 7.745966692414834 >>> LA.matrix_norm(b, ord='fro') 7.745966692414834 >>> LA.matrix_norm(b, ord=np.inf) 9.0 >>> LA.matrix_norm(b, ord=-np.inf) 2.0
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
🌐
SciPy
docs.scipy.org › doc › numpy-1.6.0 › reference › generated › numpy.linalg.norm.html
numpy.linalg.norm — NumPy v1.6 Manual (DRAFT)
numpy.linalg.norm(x, ord=None)¶ · Matrix or vector norm. This function is able to return one of seven different matrix norms, or one of an infinite number of vector norms (described below), depending on the value of the ord parameter. Notes · For values of ord <= 0, the result is, strictly ...
🌐
KDnuggets
kdnuggets.com › 2023 › 05 › vector-matrix-norms-numpy-linalg-norm.html
Vector and Matrix Norms with NumPy Linalg Norm - KDnuggets
... To sum up, when the norm() function is called with a matrix as the input, it returns the Frobenius norm of the matrix by default. To calculate the nuclear norm of a matrix, you can pass in the matrix and set ord to 'nuc' in the norm() function ...
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.linalg.norm.html
numpy.linalg.norm — NumPy v2.1 Manual
This function is able to return one of eight different matrix norms, or one of an infinite number of vector norms (described below), depending on the value of the ord parameter.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.linalg.norm.html
numpy.linalg.norm — NumPy v2.2 Manual
This function is able to return one of eight different matrix norms, or one of an infinite number of vector norms (described below), depending on the value of the ord parameter.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.linalg.vector_norm.html
numpy.linalg.vector_norm — NumPy v2.5.dev0 Manual
>>> from numpy import linalg as LA >>> a = np.arange(9) + 1 >>> a array([1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> b = a.reshape((3, 3)) >>> b array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> LA.vector_norm(b) 16.881943016134134 >>> LA.vector_norm(b, ord=np.inf) 9.0 >>> LA.vector_norm(b, ord=-np.inf) 1.0
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.linalg.norm.html
numpy.linalg.norm — NumPy v2.5.dev0 Manual
This function is able to return one of eight different matrix norms, or one of an infinite number of vector norms (described below), depending on the value of the ord parameter.
🌐
GeeksforGeeks
geeksforgeeks.org › python › find-a-matrix-or-vector-norm-using-numpy
Find a Matrix or Vector Norm using NumPy - GeeksforGeeks
December 13, 2025 - A norm measures the magnitude or length of a vector or matrix. NumPy provides the numpy.linalg.norm() function, which computes different types of vector and matrix norms depending on the parameters used.
🌐
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 - If not specified, the default is ... the entire array. You can use the numpy.linalg.norm function to calculate different types of norms for vectors and matrices:...