.norm()
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 isNone
, 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 isNone
, 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 isFalse
.
Example
The following example demonstrates a straightforward usage of the .norm()
function to compute the Frobenius norm of a matrix:
import numpy as npmatrix = 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:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours