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
scipy_linalg.htm
Advertisements