
- NumPy Tutorial
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy Compiler
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
NumPy - Matrix Norms
What are Matrix Norms?
A matrix norm is a function that assigns a non-negative number to a matrix. It provides a measure of the size or magnitude of a matrix.
In general, matrix norms are used to quantify how large or small a matrix is, and they play an important role in problems involving matrix equations, such as in solving systems of linear equations or performing matrix factorizations.
Common Types of Matrix Norms
There are several types of matrix norms, but the most commonly used ones are −
- Frobenius norm
- 1-norm
- Infinity norm
- 2-norm (spectral norm)
Frobenius Norm
The Frobenius norm is one of the simplest and most commonly used matrix norms. It is defined as the square root of the sum of the absolute squares of the matrix elements. Mathematically, it is given by −
‖A‖F = √(i=1 j=1 |aij|2)
Where A is the matrix, and aij are the elements of the matrix. The Frobenius norm is equivalent to the L2 norm of the matrix treated as a vector.
1-Norm
The 1-norm (also called the maximum column sum norm) of a matrix is defined as the maximum absolute column sum. Mathematically, it is given by −
‖A‖1 = maxj i=1 |aij|
In simple terms, the 1-norm is the maximum sum of the absolute values of the elements in any column of the matrix.
Infinity Norm
The infinity norm (also called the maximum row sum norm) of a matrix is defined as the maximum absolute row sum. Mathematically, it is given by −
‖A‖∞ = maxi j=1 |aij|
The infinity norm gives the maximum sum of absolute values of elements in any row of the matrix.
2-Norm (Spectral Norm)
The 2-norm (also called the spectral norm) of a matrix is defined as the largest singular value of the matrix. It measures the largest stretch factor of the matrix when applied to a vector. The 2-norm is given by −
‖A‖2 = max(A)
Where, max(A) is the largest singular value of matrix A. In this case, the 2-norm is related to the matrix's singular values, which can be computed using singular value decomposition (SVD).
Matrix Norms in NumPy
NumPy provides functions for calculating various matrix norms. The numpy.linalg.norm() function can be used to compute most of the common matrix norms. Let us explore how to use this function for different types of matrix norms.
Frobenius Norm Using NumPy
To compute the Frobenius norm using NumPy, we use the numpy.linalg.norm() function with the parameter ord='fro'.
Example
In the following example, the Frobenius norm of the matrix A is calculated by taking the square root of the sum of the squares of all elements in the matrix −
import numpy as np # Define a matrix A A = np.array([[1, 2], [3, 4]]) # Compute the Frobenius norm of the matrix frobenius_norm = np.linalg.norm(A, 'fro') print("Frobenius norm of A:", frobenius_norm)
Following is the output obtained −
Frobenius norm of A: 5.477225575051661
1-Norm Using NumPy
To compute the 1-norm, we use the numpy.linalg.norm() function with the parameter ord=1. The 1-norm of the matrix is the maximum sum of the absolute values of the elements in any column of the matrix.
Example
In this case, the column sums are 4 and 6, so the 1-norm is 6 −
import numpy as np # Define a matrix A A = np.array([[1, 2], [3, 4]]) # Compute the 1-norm of the matrix one_norm = np.linalg.norm(A, 1) print("1-norm of A:", one_norm)
Following is the output obtained −
1-norm of A: 6.0
Infinity Norm Using NumPy
To compute the infinity norm, we use the numpy.linalg.norm() function with the parameter ord=np.inf. The infinity norm of the matrix is the maximum sum of the absolute values of the elements in any row of the matrix.
Example
In this case, the row sums are 3 and 7, so the infinity norm is 7 −
import numpy as np # Define a matrix A A = np.array([[1, 2], [3, 4]]) # Compute the infinity norm of the matrix infinity_norm = np.linalg.norm(A, np.inf) print("Infinity norm of A:", infinity_norm)
Following is the output obtained −
Infinity norm of A: 7.0
2-Norm Using NumPy
To compute the 2-norm (spectral norm), we use the numpy.linalg.norm() function with the parameter ord=2. The 2-norm (spectral norm) of the matrix is the largest singular value of the matrix, which measures the largest stretch factor of the matrix when applied to a vector.
Example
Following is an example to compute the 2-norm in NumPy −
import numpy as np # Define a matrix A A = np.array([[1, 2], [3, 4]]) # Compute the 2-norm of the matrix two_norm = np.linalg.norm(A, 2) print("2-norm (spectral norm) of A:", two_norm)
Following is the output obtained −
2-norm (spectral norm) of A: 5.464985704219043
Applications of Matrix Norms
Matrix norms have many practical applications in numerical analysis, machine learning, optimization, and more −
- Numerical Stability: Matrix norms are used to analyze the stability of numerical algorithms, especially when solving linear systems or performing matrix factorizations.
- Machine Learning: In machine learning, matrix norms are often used to regularize models and prevent overfitting. For example, L2 regularization uses the Frobenius norm to penalize large weights in a model.
- Optimization: Matrix norms are used to measure the error or deviation from the desired solution in optimization problems.
- Signal Processing: In signal processing, matrix norms are used to measure the "energy" or magnitude of signals and filters.