Skip to main content
v2026.11,858 entries · CC-BY 4.0

numpy.linalg.norm: Syntax, ord Values and Axis Examples

How numpy.linalg.norm works: the full ord table for vectors and matrices, axis and keepdims, and the 2-D trap, plus vector_norm, lstsq and inv.

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.

  1. Vector 2-norm (default). np.linalg.norm(v). If both axis and ord are None, the 2-norm of x.ravel() is returned, so this works for any number of dimensions.
  2. Choose a norm with ord. For v = 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) and np.linalg.norm(v, 0) is 2.0 (count of non-zero entries).
  3. Work along an axis. An integer axis gives vector norms along that axis. With c = np.array([[1, 2, 3], [-1, 1, 4]]), np.linalg.norm(c, axis=0) returns [1.41421356, 2.23606798, 5.] (one norm per column) and np.linalg.norm(c, axis=1) returns [3.74165739, 4.24264069] (one per row). Add ord=1, axis=1 to get [6., 6.].
  4. Matrix norms with a 2-tuple axis. A 2-tuple axis computes matrix norms of the 2-D slices on those axes. For m = np.arange(8).reshape(2, 2, 2), np.linalg.norm(m, axis=(1, 2)) returns [3.74165739, 11.22497216].
  5. Keep dimensions for broadcasting. keepdims=True leaves the normed axes in the result as size-one dimensions so the result broadcasts against the input. For the 2 by 3 array c, np.linalg.norm(c, axis=1, keepdims=True) has shape (2, 1), so c / 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. With axis=None it 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 for ord=inf, 45.0 for ord=1 and 9.0 for ord=0.
  • numpy.linalg.matrix_norm(x, /, *, keepdims=False, ord='fro') takes an input of shape (…, M, N), meaning a stack of matrices, with ord in 1, -1, 2, -2, inf, -inf, ‘fro’ or ‘nuc’. For the matrix b above, the manual gives 7.745966692414834 by default and 7.3484692283495345 for ord=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 ValueError when x.ndim != 2. Passing 'fro' with a 1-D vector fails; omit ord to get the 2-norm instead.
  • A 2-D result you did not expect. If axis is None, a 2-D x gets a matrix norm. Pass axis=0 or axis=1 to get vector norms per column or row.
  • Arrays with three or more dimensions. When axis is None, x must be 1-D or 2-D unless ord is None. Give an explicit axis (an integer or a 2-tuple) for stacks, or use matrix_norm on shape (…, M, N).
  • ord below 1. The manual notes that for values of ord below 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=True if 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

Related: numpy.linspace for building the evenly spaced arrays these functions often act on.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about numpy.linalg.norm: Syntax, ord Values and Axis Examples

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.