Written and maintained by CASRAI Editorial Board
Last updated
numpy.linalg.norm returns a vector norm or a matrix norm of an array, and which one you get depends on the ord and axis arguments. With no arguments it returns the 2-norm (Euclidean length) of the flattened array. A 2-D array with ord=2 is treated as a matrix, not as a long vector, so norm(b, 2) and norm(b.ravel(), 2) give different answers. This guide covers that trap, the full ord table, the newer vector_norm and matrix_norm functions, and how norms connect to numpy.linalg.lstsq and numpy.linalg.inv.
What numpy linalg norm is used for in research
A norm turns an array into one non-negative number that measures its size. In research code that number is a distance between two measurement vectors, the length of a residual, the size of a coefficient vector, or a convergence check. The NumPy manual describes the function as returning one of eight different matrix norms, or one of an infinite number of vector norms, depending on the value of ord.
The same idea underlies other CASRAI guides: a distance between points in the Mahalanobis distance guide generalises the Euclidean 2-norm, and norms of coefficient or data matrices appear when checking perfect multicollinearity and working with high-dimensional data.
Install or access
numpy.linalg.norm ships with NumPy; there is nothing separate to install. The official NumPy install page (numpy.org/install) gives this command for pip, ideally inside a virtual environment:
pip install numpy
With conda the same page gives conda install numpy from the defaults or conda-forge channels. Then import it as usual:
import numpy as np
np.linalg.norm([3, 4]) # 5.0
Step-by-step: computing a norm with numpy.linalg.norm
The signature in the NumPy manual is numpy.linalg.norm(x, ord=None, axis=None, keepdims=False). The steps below build up from the default call.
- Vector 2-norm (default).
np.linalg.norm(v). If bothaxisandordareNone, the 2-norm ofx.ravel()is returned, so this works for any number of dimensions. - Choose a norm with
ord. Forv = np.array([3, 4, 0]):np.linalg.norm(v, 1)is 7.0 (sum of absolute values),np.linalg.norm(v, np.inf)is 4.0 (largest absolute value) andnp.linalg.norm(v, 0)is 2.0 (count of non-zero entries). - Work along an axis. An integer
axisgives vector norms along that axis. Withc = np.array([[1, 2, 3], [-1, 1, 4]]),np.linalg.norm(c, axis=0)returns[1.41421356, 2.23606798, 5.](one norm per column) andnp.linalg.norm(c, axis=1)returns[3.74165739, 4.24264069](one per row). Addord=1, axis=1to get[6., 6.]. - Matrix norms with a 2-tuple axis. A 2-tuple
axiscomputes matrix norms of the 2-D slices on those axes. Form = np.arange(8).reshape(2, 2, 2),np.linalg.norm(m, axis=(1, 2))returns[3.74165739, 11.22497216]. - Keep dimensions for broadcasting.
keepdims=Trueleaves the normed axes in the result as size-one dimensions so the result broadcasts against the input. For the 2 by 3 arrayc,np.linalg.norm(c, axis=1, keepdims=True)has shape (2, 1), soc / np.linalg.norm(c, axis=1, keepdims=True)scales every row to unit length.
Output and how to read it: the ord table
The function returns a float or an ndarray. The table below is the ord table from the NumPy manual’s numpy.linalg.norm page (NumPy v2.5 documentation, checked 2026-10-29).
ord |
Norm for matrices (2-D, axis=None) | Norm for vectors |
|---|---|---|
| None | Frobenius norm | 2-norm |
| ‘fro’ | Frobenius norm | not defined |
| ‘nuc’ | Nuclear norm | not defined |
| inf | max(sum(abs(x), axis=1)) | max(abs(x)) |
| -inf | min(sum(abs(x), axis=1)) | min(abs(x)) |
| 0 | not defined | sum(x != 0) |
| 1 | max(sum(abs(x), axis=0)) | sum(abs(x)**ord)**(1./ord) |
| -1 | min(sum(abs(x), axis=0)) | sum(abs(x)**ord)**(1./ord) |
| 2 | Largest singular value | sum(abs(x)**ord)**(1./ord) |
| -2 | Smallest singular value | sum(abs(x)**ord)**(1./ord) |
| other | not defined | sum(abs(x)**ord)**(1./ord) |
The Frobenius norm is the square root of the sum of squared absolute entries, and the nuclear norm is the sum of the singular values.
The 2-D trap: the same numbers, two different 2-norms
Let a = np.arange(9) - 4 and b = a.reshape((3, 3)). These results are the NumPy manual’s own examples:
| Call | Result | What it computes |
|---|---|---|
norm(a) and norm(b) |
7.745966692414834 | vector 2-norm, and Frobenius norm of the matrix (equal) |
norm(b, 2) |
7.3484692283495345 | largest singular value of the matrix |
norm(a, np.inf) |
4.0 | largest absolute entry |
norm(b, np.inf) |
9.0 | largest absolute row sum |
norm(a, 1) |
20.0 | sum of absolute values |
norm(b, 1) |
7.0 | largest absolute column sum |
So with axis=None, a 2-D input is always read as a matrix once you pass an ord. If you want the vector 2-norm of every entry in a 2-D array, use np.linalg.norm(b.ravel(), 2) or the default call with no ord.
vector_norm and matrix_norm: the explicit alternatives
NumPy also documents two functions that remove the guesswork, each with a fixed meaning:
numpy.linalg.vector_norm(x, /, *, axis=None, keepdims=False, ord=2)is described as Array API compatible. Withaxis=Noneit works over all values (the array is flattened). For the 3 by 3 array of the integers 1 to 9, the manual gives 16.881943016134134 for the default, 9.0 forord=inf, 45.0 forord=1and 9.0 forord=0.numpy.linalg.matrix_norm(x, /, *, keepdims=False, ord='fro')takes an input of shape (…, M, N), meaning a stack of matrices, withordin 1, -1, 2, -2, inf, -inf, ‘fro’ or ‘nuc’. For the matrixbabove, the manual gives 7.745966692414834 by default and 7.3484692283495345 forord=2.
The practical difference: vector_norm on a 3 by 3 array of 1 to 9 returns 16.88, the flattened 2-norm, whereas np.linalg.norm with ord=2 on the same array would return the largest singular value. Pick the function that names what you mean.
Norms in least squares and matrix inversion
The variants people search alongside numpy linalg norm are mostly norm users.
numpy linalg lstsq
numpy.linalg.lstsq(a, b, rcond=None) returns x, residuals, rank, s. The solution x minimises the Euclidean 2-norm of b - a @ x, and among multiple minimisers it returns the one with the smallest 2-norm. The residuals output is the squared Euclidean 2-norm for each column of b - a @ x, and it is an empty array if the rank of a is less than N or if M is not greater than N. In that empty case, norm is how you recover the residual yourself:
x = np.array([0, 1, 2, 3])
y = np.array([-1, 0.2, 0.9, 2.1])
A = np.vstack([x, np.ones(len(x))]).T
sol, res, rank, s = np.linalg.lstsq(A, y)
np.linalg.norm(y - A @ sol)**2 # equals res[0], about 0.05
The manual’s version of this example gives a slope and intercept of approximately 1.0 and -0.95. The manual also notes that rcond‘s default changed in NumPy 2.0: the previous default was -1, and it is now machine precision times max(M, N).
numpy linalg inv
numpy.linalg.inv(a) takes an array of shape (…, M, M). It raises LinAlgError if the input is not square or the inversion fails, which includes singular matrices. For a nearly singular (ill-conditioned) matrix the manual says the error may or may not be raised and the result may be inaccurate because of floating-point error. A norm-based check such as np.linalg.norm(A @ A_inv - np.eye(n)) against zero is a simple way to see how far the product is from the identity.
Troubleshooting
- ValueError with ‘fro’ or ‘nuc’. Both are defined only for matrices and raise a
ValueErrorwhenx.ndim != 2. Passing'fro'with a 1-D vector fails; omitordto get the 2-norm instead. - A 2-D result you did not expect. If
axisis None, a 2-Dxgets a matrix norm. Passaxis=0oraxis=1to get vector norms per column or row. - Arrays with three or more dimensions. When
axisis None,xmust be 1-D or 2-D unlessordis None. Give an explicitaxis(an integer or a 2-tuple) for stacks, or usematrix_normon shape (…, M, N). - ord below 1. The manual notes that for values of
ordbelow 1 the result is, strictly speaking, not a mathematical norm. - ord=0 is a count, not a length. For vectors it returns the number of non-zero entries.
- Shapes after normalising. Use
keepdims=Trueif you divide the array by its norms, or the shapes will not broadcast.
Free alternatives
| Option | What it does |
|---|---|
numpy.linalg.norm |
One function for vector and matrix norms; behaviour set by ord and axis. |
numpy.linalg.vector_norm |
Vector norms only; default ord=2; flattens when axis=None. |
numpy.linalg.matrix_norm |
Matrix norms only, over stacks of matrices; default ord='fro'. |
scipy.linalg.norm |
The NumPy manual lists it under See Also as a similar function in SciPy. |
np.sqrt(np.sum(x**2)) |
The 2-norm of a real array written out as arithmetic. |
FAQ
What does norm numpy return by default?
The 2-norm of the flattened array. For [3, 4] it returns 5.0.
Is numpy norm the same as numpy.linalg.norm?
People search for “numpy norm” and “norm numpy” meaning numpy.linalg.norm, which is the function documented in the NumPy manual. Newer code can also use numpy.linalg.vector_norm or numpy.linalg.matrix_norm.
How do I get the L1 norm?
Pass ord=1 to a vector: it returns the sum of absolute values. On a 2-D array the same call returns the largest absolute column sum, so use b.ravel() or vector_norm(b, ord=1) for the sum over all entries.
How does numpy linalg lstsq relate to norms?
It finds the x that minimises the 2-norm of b - a @ x, and its residuals output is that norm squared.
What happens with numpy linalg inv on a singular matrix?
It raises LinAlgError. For an ill-conditioned matrix the error may or may not be raised and the result may be inaccurate.
Sources
- numpy.linalg.norm, NumPy v2.5 manual (checked 2026-10-29)
- numpy.linalg.vector_norm, NumPy v2.5 manual (checked 2026-10-29)
- numpy.linalg.matrix_norm, NumPy v2.5 manual (checked 2026-10-29)
- numpy.linalg.lstsq, NumPy v2.5 manual (checked 2026-10-29)
- numpy.linalg.inv, NumPy v2.5 manual (checked 2026-10-29)
- Installing NumPy, numpy.org (checked 2026-10-29)
Related: numpy.linspace for building the evenly spaced arrays these functions often act on.








