.norm()

Anonymous contributor's avatar
Anonymous contributor
Published Apr 7, 2024
Contribute to Docs

In NumPy, the .norm() function computes the norm of a matrix, either across the entire array or along a specified axis. It helps identify differences between matrices, pinpoint predictive errors, manage model complexity, and validate numerical algorithms.

Syntax

numpy.linalg.norm(a, ord=None, axis=None, keepdims=False)
  • a: The input array for which the norm is computed.
  • ord=None (Optional): Specifies the order of the norm to compute. The default is None, which computes the Frobenius norm for matrices and 2-norm for vectors.
  • axis=None (Optional): Specifies the axis or axes along which to compute the norm. The default is None, which computes the norm over the entire array.
  • keepdims=False (Optional): Specifies whether to keep the dimensions of the original array in the result. The default is False.

Example

The following example demonstrates a straightforward usage of the .norm() function to compute the Frobenius norm of a matrix:

import numpy as np
matrix = np.array([[1, 2], [3, 4]])
frobenius_norm = np.linalg.norm(matrix)
print("Frobenius norm of the matrix:", frobenius_norm)

This produces the following output:

Frobenius norm of the matrix: 5.477225575051661

Codebyte Example

The following example computes different norms for a vector using the .norm() function with various values of the ord parameter, including 1-norm (Manhattan), 2-norm (Euclidean), and infinity norm:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python:NumPy on Codecademy