
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
SciPy - norm() Function
A norm is a mathematical concept which gives the "size" or "length" of a vector or matrix. It calculates an object's size and length using its magnitude. Norms are used in linear algebra to analyze distances, mistakes, and system stability.
The scipy.linalg.norm() calculates the norm of a vector or matrix. This function does support alternative norms for measurements of size and length by the demands of an application. It is very highly used in mathematical and engineering computations.
Common norms include the L2 or Euclidean norm, which calculates distance along a straight line (e.g, GPS navigation), and the L1 or Manhattan norm, which sums the absolute differences of elements (e.g, taxi fares). For matrices, the Frobenius norm is used, which is like the L2 norm but sums the squares of all elements in the matrix.
Syntax
The syntax for the SciPy norm() method is as follows −
.norm(a, ord=None, axis=None, keepdims=False, check_finite=True)
Parameters
This method accepts the following parameters −
a (array-like) − Input array (vector or matrix) for which the norm is calculated.
ord (int, inf, -inf, fro, nuc, None), optional − The ord parameter defines the norm type: for vectors (1, 2, np.inf, -np.inf) and matrices ('fro', 1, 2, np.inf).
axis (int, 2-tuple of ints, None) optional − Specifies axes along which the norm is computed. If None, computes the norm of the flattened array.
keepdims (bool, optional) − If True, retains reduced dimensions in the result.
check_finite (bool, optional) − Ensures input has finite values. If False, skips this check for performance.
Return Value
n: (float or ndarray) − The computed norm value of the input array.
Example 1: Calculate L2 Norm of a Vector
The ord parameter in the norm() function lets you choose different norm types for vectors or matrices, like L1, L2, or Frobenius norm.
This below code uses scipy.linalg.norm() to calculate the L2 norm also known as Euclidean norm, of the vector [3 4]. This helps to determine the vector's "length" or magnitude. It measures the straight-line distance between the origin and a point in space.
import numpy as np import scipy.linalg vector = np.array([3, 4]) vector_norm = scipy.linalg.norm(vector, ord = 2) print("L2 norm of vector:", vector_norm)
When we run above program, it produces following result
L2 norm of vector: 5.0
Example 2: Compute Frobenius Norm for Matrix Magnitude.
The Frobenius norm calculates the overall "size" or "magnitude" of a matrix by adding up the squares of its elements.
In the code below, we calculate the Frobenius norm of the matrix [[1 2], [3 4]] using norm(). This measurement plays a key role in figuring out a matrix's magnitude.
import numpy as np from scipy.linalg import norm matrix = np.array([[1, 2], [3, 4]]) matrix_norm = norm(matrix, ord = 'fro') # Calculates the Frobenius norm print("Frobenius norm of matrix:", matrix_norm)
Following is an output of the above code
Frobenius norm of matrix: 5.477225575051661
Example 3: L1 Norm for Vector Sum.
In the below code we compute the L1 norm of this vector [1,2], which sums up all the absolute values of components using norm() method. The L1 norm is frequently applied in regularizations and sparse solutions optimization.
import numpy as np from scipy.linalg import norm vector = np.array([1, 2]) vector_norm = norm(vector, ord=1) # L1 norm (Manhattan norm) print("L1 norm of vector:", vector_norm)
Output of the above code is as follows
L1 norm of vector: 3.0
Example 4: Norm Calculation
The norm determines the length or magnitude of the solution vector x, which is the dimension in geometric space. It means that the vector is closer to the origin in space geometry when the norm value is smaller.
The following example creates the matrix A, solves the system Ax=b using solve() method, to find x and calculates the magnitude using the method scipy.linalg.norm() in order to find the norm.
import numpy as np from scipy.linalg import solve, norm A = np.array([[1, 2], [3, 4]]) b = np.array([6, 14]) # Solve and compute the norm of the solution x = solve(A, b) solution_norm = norm(x) print("Solution x:", x) print("Norm of the solution:", solution_norm)
Output of the above code is as follows
Solution x: [2. 2.] Norm of the solution: 2.82842712474619