API · NumPy
Python NumPy numpy.linalg.norm() Function
Learn how numpy.linalg.norm() computes vector and matrix norms and how ord, axis, and keepdims shape the result.
On this page
Use numpy.linalg.norm() to compute a norm from an array. The input shape and arguments determine whether the call calculates a vector norm, a matrix norm, or a set of norms along selected axes. The ord argument selects the norm definition; it does not decide whether the input is treated as a vector or a matrix.
Syntax and parameters of numpy.linalg.norm()
The function signature is:
numpy.linalg.norm(x, ord=None, axis=None, keepdims=False)
xis the array-like input whose norm is calculated.ordselects the norm definition. Its default,None, uses the Euclidean norm for a vector and the Frobenius norm for a matrix. Supported values depend on whether the selected calculation is a vector or matrix norm; see the NumPy reference for the full list.axischooses the calculation axes. An integer selects one axis and computes vector norms along it. A two-integer tuple selects two axes and computes matrix norms over those axes. When bothaxis=Noneandord=None, NumPy computes the norm of the flattened input, including inputs with more than two dimensions. For a higher-dimensional input, pass an integeraxisto compute vector norms along that axis, or a two-axis tuple to compute matrix norms over those axes for each remaining slice.keepdimsdefaults toFalse. WhenTrue, axes used for the calculation remain in the result with size one, which can help with broadcasting against the original array.
Without axis selection, the result is a scalar for a vector or matrix norm. With axis selecting multiple slices, the result is an array containing one norm for each slice; keepdims=True retains the reduced axes as dimensions of size one.
Compute vector and matrix norms
For a one-dimensional input, the default is the Euclidean vector norm: the square root of the sum of the squared elements. For a two-dimensional input, the default is the Frobenius matrix norm, which applies the same calculation across all matrix entries. This standalone example shows both calculations:
import numpy as np
vector = np.array([3, 4])
matrix = np.array([[3, 4], [0, 12]])
print(np.linalg.norm(vector))
print(np.linalg.norm(matrix))
5.0
13.0
The first scalar is the Euclidean norm of the vector [3, 4]. The second is the Frobenius norm of the matrix, calculated across all four entries.
Select a norm with ord
For vectors, ord selects common definitions such as 2 for the Euclidean norm, 1 for the sum of absolute values, and np.inf for the largest absolute element. For matrices, ord="fro" selects the Frobenius norm, while ord="nuc" selects the nuclear norm, the sum of the matrix’s singular values. The spelling is nuc; nuuc is not a supported alias.
The example below compares the Frobenius and nuclear norms of the same matrix:
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
print(np.linalg.norm(matrix, ord="fro"))
print(np.linalg.norm(matrix, ord="nuc"))
5.477225575051661
5.8309518948453
The first value is the Frobenius norm; the second is the nuclear norm, the sum of the matrix’s singular values.
Choose a matrix ord value for a matrix calculation. If the input has more than two dimensions, select axes explicitly when the desired operation is a vector or matrix norm; the supported combinations depend on the selected norm definition and NumPy version.
Choose axes and retain dimensions
For an array shaped (rows, columns), axis=0 computes a vector norm for each column, while axis=1 computes one for each row. To calculate a matrix norm over both dimensions, pass axis=(0, 1):
import numpy as np
matrix = np.array([[3, 4], [0, 12]])
print(np.linalg.norm(matrix, axis=0))
print(np.linalg.norm(matrix, axis=1))
print(np.linalg.norm(matrix, axis=(0, 1), keepdims=True).shape)
[ 3. 12.64911064]
[ 5. 12.]
(1, 1)
The first array contains the vector norms of the two columns, and the second contains the vector norms of the two rows. The final tuple is the shape of the matrix norm result with keepdims=True; the reduced axes remain as dimensions of size one for later broadcasting. For higher-dimensional arrays, choose the axes that form each vector or matrix slice.