Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A norm turns “size” into a number, but the right number depends on what you want to measure: a vector’s length, total absolute error, a matrix’s entry magnitude, or the greatest amount a transformation can stretch a vector. NumPy’s np.linalg.norm() handles both vectors and matrices, with meaning that changes according to the input shape and arguments. In current NumPy 2.x, np.linalg.vector_norm() and np.linalg.matrix_norm() make that intent explicit.

Use vector_norm for vectors and batches of vectors, and matrix_norm for matrices and stacks of matrices. Choose the order—such as 1, 2, infinity, Frobenius, or nuclear—based on the question you need the result to answer.

What does a norm measure?

A norm is a function that measures the size of a vector or matrix. A mathematical norm has four properties:

  • Non-negativity: ||x|| >= 0.
  • Definiteness: ||x|| = 0 only when x is the zero vector.
  • Absolute homogeneity: scaling by alpha scales the norm by |alpha|.
  • Triangle inequality: ||x + y|| <= ||x|| + ||y||.

A norm measures one object’s size. To measure distance between two vectors, take the norm of their difference: np.linalg.vector_norm(x - y).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Not every value accepted by NumPy’s ord parameter defines a mathematical norm. In particular, ord=0 counts nonzero entries, and orders below 1 do not satisfy the standard norm properties. NumPy documents these distinctions in its norm reference.

Compute vector norms with NumPy

For an ordinary vector, NumPy’s default is the Euclidean (L2) norm:

import numpy as np

x = np.array([3, 4])

np.linalg.norm(x)
# 5.0

np.linalg.vector_norm(x, ord=2)
# 5.0

The explicit vector_norm function defaults to ord=2. Its general form is (sum(abs(x_i) ** p)) ** (1 / p) for positive p.

Common vector orders

Order Definition or result Useful for
1 Sum of absolute values Total absolute magnitude; L1 error or regularization
2 Square root of the sum of squared absolute values Euclidean length, least squares, normalization
np.inf Largest absolute component Worst individual error or component limit
-np.inf Smallest absolute component Diagnosing the smallest component
0 Count of nonzero components Support size; technically not a norm

For example, with x = np.array([-4, 3, 0, 2]), the 0-order count is 3, the L1 norm is 9, the L2 norm is about 5.385, the infinity norm is 4, and the negative-infinity result is 0. These values answer different questions rather than competing to be the one “correct” size.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
x = np.array([-4, 3, 0, 2])

for order in [0, 1, 2, np.inf, -np.inf]:
    print(order, np.linalg.vector_norm(x, ord=order))

Use np.count_nonzero(x) in production code when you mean a nonzero-entry count; that expresses the intent more clearly than calling it an L0 norm. A positive non-integer order such as 3 is also supported for vector calculations.

Compute matrix norms—and distinguish them from vector norms

For a two-dimensional array, the generic np.linalg.norm() default is the Frobenius norm: the square root of the sum of squared absolute entries. It has the same numeric result as flattening all entries and taking an L2 norm, but it is interpreted as a matrix norm.

A = np.array([[1, 2],
              [3, 4]])

np.linalg.norm(A)
# about 5.477

np.linalg.matrix_norm(A, ord="fro")
# about 5.477

Use matrix_norm to state clearly that the final two dimensions represent a matrix. NumPy’s matrix_norm reference documents its matrix orders and stack behavior.

Matrix order meanings

ord Matrix interpretation
None or "fro" Frobenius norm: square root of the sum of squared absolute entries
"nuc" Nuclear norm: sum of the singular values
1 Maximum absolute column sum
-1 Minimum absolute column sum
np.inf Maximum absolute row sum
-np.inf Minimum absolute row sum
2 Spectral norm: largest singular value
-2 Smallest singular value

Column sums and row sums

For this matrix, the 1-norm is the largest absolute column sum, while the infinity norm is the largest absolute row sum:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
A = np.array([[1, -2],
              [3,  4]])

np.linalg.matrix_norm(A, ord=1)
# 6.0: max(1 + 3, 2 + 4), the column sums

np.linalg.matrix_norm(A, ord=np.inf)
# 7.0: max(1 + 2, 3 + 4), the row sums

Reversing rows and columns is a common source of errors: ord=1 is column-based, and ord=np.inf is row-based.

Spectral and nuclear norms

The spectral norm is the largest singular value. Equivalently, it is the greatest factor by which the matrix can stretch any vector measured with the L2 norm:

||A||_2 = max over x != 0 of ||Ax||_2 / ||x||_2

spectral_norm = np.linalg.matrix_norm(A, ord=2)
smallest_singular_value = np.linalg.matrix_norm(A, ord=-2)
singular_values = np.linalg.svdvals(A)

The nuclear norm is the sum of singular values, not a sum of the matrix entries. It is used in areas such as low-rank approximation, matrix completion, and optimization methods that use nuclear-norm regularization as a convex approach to encouraging low rank. NumPy’s linear algebra routine list includes singular-value routines.

Choose between norm, vector_norm, and matrix_norm

Function Use it when
np.linalg.norm(x, ord=2, axis=1) You need the generic API, for example to support older NumPy code, or the input’s vector-versus-matrix role is already clear.
np.linalg.vector_norm(x, ord=2, axis=1) The data represents one or more vectors and you want vector semantics to be explicit.
np.linalg.matrix_norm(A, ord="fro") The final two dimensions represent a matrix and you want a matrix-specific calculation.

The generic norm function selects vector or matrix behavior based on dimensionality and arguments. With both ord and axis left as None, a higher-dimensional input is flattened before its default 2-norm calculation. Explicit functions help prevent that context-dependent interpretation from being accidental. See the vector_norm reference for vector and batch semantics.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The current NumPy 2.x documentation gives these signatures:

np.linalg.norm(x, ord=None, axis=None, keepdims=False)
np.linalg.vector_norm(x, /, *, axis=None, keepdims=False, ord=2)
np.linalg.matrix_norm(x, /, *, keepdims=False, ord="fro")

For compatibility with older NumPy versions, check that vector_norm and matrix_norm are available in your target environment; use norm when necessary.

Use axis and keepdims for batches

For a two-dimensional array, axis=1 reduces across each row and returns one norm per row; axis=0 reduces down each column and returns one norm per column.

Rank #4
Sale
Linear Algebra 5th Edition
  • Brand: Pearson Education
  • Linear Algebra 5th Edition
X = np.array([[3, 4],
              [5, 12]])

X.shape
# (2, 2)

np.linalg.vector_norm(X, axis=1)
# array([ 5., 13.])

np.linalg.vector_norm(X, axis=0)
# array([ 5.83095189, 12.64911064])

When samples are stored as rows, normalize each row with axis=1. If samples are stored as columns, the corresponding reduction is axis=0.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
embeddings = np.array([[3.0, 4.0],
                       [5.0, 12.0],
                       [8.0, 15.0]])

lengths = np.linalg.vector_norm(embeddings, axis=1)
# array([ 5., 13., 17.])

normalized = embeddings / lengths[:, None]

With keepdims=True, the reduced dimension stays in the result with size one. This makes broadcasting straightforward:

lengths = np.linalg.vector_norm(
    embeddings,
    axis=1,
    keepdims=True
)
normalized = embeddings / lengths

For arrays shaped (batch, rows, columns), matrix_norm treats the last two dimensions as the matrix and calculates one result per leading batch item. The generic alternative identifies the matrix dimensions with a tuple of axes:

batch = np.arange(8).reshape(2, 2, 2)

np.linalg.matrix_norm(batch, ord="fro")
np.linalg.norm(batch, ord="fro", axis=(1, 2))

Apply norms to errors, normalization, and matrix behavior

Compare prediction errors

Given an error vector, L1 reports total absolute deviation, L2 combines deviations with greater emphasis on large components, and infinity reports only the worst component:

error = prediction - target

l1_error = np.linalg.vector_norm(error, ord=1)
l2_error = np.linalg.vector_norm(error, ord=2)
worst_error = np.linalg.vector_norm(error, ord=np.inf)

Choose the measure that matches the requirement. If no individual error may exceed a tolerance, the infinity norm directly checks the maximum absolute component; it does not summarize the total error.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Normalize a single vector

x = np.array([3.0, 4.0])
unit_x = x / np.linalg.vector_norm(x)
# array([0.6, 0.8])

The result has unit L2 length as long as the input vector is nonzero.

Measure matrix amplification

If the question is how much a linear transformation can stretch a vector, use the spectral norm rather than an entrywise measure such as Frobenius:

A = np.array([[1.0, 2.0],
              [3.0, 4.0]])
spectral_norm = np.linalg.matrix_norm(A, ord=2)

Check sensitivity with a condition number

A norm alone does not establish that a numerical solution is stable. A small residual can coexist with a large solution error when the system is sensitive to perturbations. NumPy provides np.linalg.cond(A), with choices including p=1, p=2, and p=np.inf, to calculate a condition number. Use it alongside residual measurements when sensitivity matters; see the condition-number reference.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Avoid common norm calculation mistakes

  • Assuming np.linalg.norm(A) is always a vector L2 norm: for a 2D input, the default is the Frobenius matrix norm. Use vector_norm when treating the entries as a vector, or specify matrix_norm(A, ord=2) for spectral norm.
  • Normalizing along the wrong axis: for row-stored samples, use axis=1; axis=0 instead normalizes columns.
  • Omitting keepdims: the reduced result may not broadcast against the original array as intended. Keep the reduced dimension when dividing an array by its per-row or per-column norms.
  • Dividing by a zero norm: zero vectors have norm zero. Choose whether to preserve, remove, or reject them rather than allowing invalid values to emerge silently.
  • Calling every order a true norm: ord=0 is a count, and negative orders are numerical quantities with specialized meanings. Use precise labels.
  • Using matrix-only orders on vectors: "fro" and "nuc" are matrix options, not vector orders.
  • Reading a large result as proof of an error: magnitude depends on element count, units, feature scales, and the chosen order.
  • Ignoring numerical scale and dtype: very large or small values and low-precision types can lead to overflow, underflow, or precision loss. Check the input dtype, scale data where appropriate, and compare floating-point results with tolerances rather than exact equality.

To normalize a batch while leaving zero rows as zero, use a guarded divide:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
norms = np.linalg.vector_norm(X, axis=1, keepdims=True)

normalized = np.divide(
    X,
    norms,
    out=np.zeros_like(X, dtype=float),
    where=norms != 0
)

Choose an order for the question you are asking

Question Calculation
How long is this vector? np.linalg.vector_norm(x)
What is its total absolute magnitude? np.linalg.vector_norm(x, ord=1)
What is its largest absolute component? np.linalg.vector_norm(x, ord=np.inf)
How many values are nonzero? np.count_nonzero(x)
What is the entrywise size of this matrix? np.linalg.matrix_norm(A, ord="fro")
What is its largest absolute column sum? np.linalg.matrix_norm(A, ord=1)
What is its largest absolute row sum? np.linalg.matrix_norm(A, ord=np.inf)
How much can it amplify an L2 vector? np.linalg.matrix_norm(A, ord=2)
What is its smallest singular value? np.linalg.matrix_norm(A, ord=-2)
What is the sum of its singular values? np.linalg.matrix_norm(A, ord="nuc")
How sensitive is a linear system? np.linalg.cond(A, p=...)

Runnable example

This script prints common vector and matrix measures and demonstrates row-wise normalization:

Quick Recap

SaleBestseller No. 4
Linear Algebra 5th Edition
Linear Algebra 5th Edition
Brand: Pearson Education; Linear Algebra 5th Edition
$26.68
import numpy as np

x = np.array([3.0, 4.0])
A = np.array([[1.0, 2.0],
              [3.0, 4.0]])

print("Vector L1:", np.linalg.vector_norm(x, ord=1))
print("Vector L2:", np.linalg.vector_norm(x, ord=2))
print("Vector infinity:", np.linalg.vector_norm(x, ord=np.inf))
print("Matrix Frobenius:", np.linalg.matrix_norm(A, ord="fro"))
print("Matrix L1:", np.linalg.matrix_norm(A, ord=1))
print("Matrix infinity:", np.linalg.matrix_norm(A, ord=np.inf))
print("Matrix spectral:", np.linalg.matrix_norm(A, ord=2))
print("Matrix nuclear:", np.linalg.matrix_norm(A, ord="nuc"))

rows = np.array([[3.0, 4.0],
                 [5.0, 12.0]])
row_norms = np.linalg.vector_norm(rows, axis=1, keepdims=True)
print("Normalized rows:n", rows / row_norms)

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.