Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.linalg.norm

Computes vector or matrix norms for an array-like input. The norm type and dimensions being reduced are controlled by ord and axis.

How it works

Convert an array-like input into vector or matrix norms. Choose the norm order with ord, select vector or matrix axes with axis, and use keepdims to preserve reduced axes for broadcasting.

Syntax

numpy.linalg.norm(x, ord=None, axis=None, keepdims=False)

Parameters

NameTypeDescription
xarray_likeInput array.
ord{int, float, inf, -inf, 'fro', 'nuc'}, optionalOrder of the norm; supported values depend on whether a vector or matrix norm is selected.
axis{None, int, 2-tuple of ints}, optionalAxis selection: None, one integer for vector norms, or a two-integer tuple for matrix norms.
keepdimsbool, optionalWhether normed axes remain in the output with size one.

Returns

nfloat or ndarray — Norm of the matrix or vector(s).

Key points

  • The function supports several matrix norms and an unbounded family of vector norms, selected through ord.
  • ord accepts integer or floating-point orders, infinity values, and the named matrix orders 'fro' and 'nuc', subject to the documented matrix/vector combinations.
  • keepdims=True preserves the reduced axes as length-one dimensions, which is useful when the result must broadcast against the original array.
  • The return is a floating-point scalar or an ndarray, depending on whether the selected computation produces one norm or multiple norms.

Common mistakes

  • With both axis and ord omitted, the input is flattened before its vector 2-norm is computed.
  • Leaving axis unset makes the interpretation depend on dimensionality: one-dimensional inputs use vector norms, while two-dimensional inputs use matrix norms.
  • For batched vectors, an integer axis selects the dimension reduced by the vector norm.
  • A two-axis tuple treats those dimensions as matrices and computes a matrix norm for each remaining batch position.

Example: Normalize a batch of vectors

import numpy as np

vectors = np.array([[3.0, 4.0], [5.0, 12.0]])
lengths = np.linalg.norm(vectors, axis=1, keepdims=True)
unit_vectors = vectors / lengths
print(lengths.ravel())
print(unit_vectors)

Computes each row's Euclidean length, retains a singleton dimension for broadcasting, and normalizes the rows.

When to use it

Vector normalization

Normalize feature vectors, embeddings, or geometric directions by dividing each vector by its length.

Matrix analysis

Measure matrix size using Frobenius, nuclear, or induced norms selected by ord.

Batch distance or magnitude calculations

Compute one norm per vector in a batched array by selecting the vector axis.

Interview questions

QWhat does numpy.linalg.norm compute?

It accepts an array-like x and returns a scalar or array containing vector or matrix norms. The selected norm depends on ord and, when supplied, axis.

Follow-up: How would you compute norms for rows in a two-dimensional array? Use axis=1; this selects the row dimension for vector norms and returns one value per row.

QWhat is the default behavior of axis and ord?

With axis=None, a one-dimensional x produces a vector norm and a two-dimensional x produces a matrix norm. If both axis and ord are None, x is flattened and its 2-norm is returned.

Follow-up: What is the default matrix norm in this case? For a two-dimensional input with ord=None, the documented table identifies it as the Frobenius norm.

QHow does ord affect the result?

ord selects the norm order, and supported values include integers, positive or negative infinity, 'fro', and 'nuc'. The page documents different supported combinations for matrices and vectors.

Follow-up: Which named orders are matrix-specific? 'fro' selects the Frobenius norm and 'nuc' selects the nuclear norm; these are not vector norm choices.

  • numpy.linalg.vector_norm
  • numpy.linalg.matrix_norm
  • numpy.linalg.svd

Source: numpy.linalg.norm. Checked 2026-10-15.