Skip to content
LearnPY
NumPyfunction reference

np.linalg.norm

Measure the length of a vector or matrix.

Signature

np.linalg.norm(x, ord=None, axis=None)

What it does

np.linalg.norm computes vector and matrix norms. Row-wise L2 norms are the usual way to build distance features, normalize embeddings, or check how far a prediction moved from a baseline.

Parameters

xarray_like
Vector or matrix to measure.
ordint or str, optionaloptional
Norm order; default is the L2 norm for vectors.
axisint or tuple, optionaloptional
Axis or axes to reduce.

Returns

ndarray or float

Example

pythonnp.linalg.norm example
import numpy as np

vec = np.array([[3.0, 4.0], [5.0, 12.0]])
print(np.linalg.norm(vec, axis=1))

Output

[ 5. 13.]

Watch out for

  • A huge norm can mean a real outlier or an unscaled feature; check units.
  • For squared distances skip the square root and save a little work.