Numpy linalg.svd: Singular Value Decomposition in Python

Numpy Linalg Svd Singular Value Decomposition

In mathematics, a singular value decomposition (SVD) of a matrix refers to the factorization of a matrix into three separate matrices. It is a more generalized version of an eigenvalue decomposition of matrices. It is further related to the polar decompositions.

In Python, it is easy to calculate the singular decomposition of a complex or a real matrix using the numerical python or the numpy library. The numpy library consists of various linear algebraic functions including one for calculating the singular value decomposition of a matrix.

In machine learning models, singular value decomposition is widely used to train models and in neural networks. It helps in improving accuracy and in reducing the noise in data. Singular value decomposition transforms one vector into another without them necessarily having the same dimension. Hence, it makes matrix manipulation in vector spaces easier and efficient. It is also used in regression analysis.

Syntax of Numpy linalg.svd() function

The function that calculates the singular value decomposition of a matrix in python belongs to the numpy module, named linalg.svd() .

The syntax of the numpy linalg.svd () is as follows:

numpy.linalg.svd(A, full_matrices=True, compute_uv=True, hermitian=False)

You can customize the true and false boolean values based on your requirements.

The parameters of the function are given below:

  • A->array_like: This is the required matrix whose singular value decomposition is being calculated. It can be real or complex as required. It’s dimension should be >= 2.
  • full_matrices->boolean value(optional): If set to true, then the Hermitian transpose of the given matrix is a square, if it’s false then it isn’t.
  • compute_uv->boolen value(optional): It determines whether the Hermitian transpose is to be calculated or not in addition to the singular value decomposition.
  • hermitian->boolean value(optional): The given matrix is considered hermitian(that is symmetric, with real values) which might provide a more efficient method for computation.

The function returns three types of matrices based on the parameters mentioned above:

  • S->array_like: The vector containing the singular values in the descending order with dimensions same as the original matrix.
  • u->array_like: This is an optional solution that is returned when compute_uv is set to True. It is a set of vectors with singular values.
  • v-> array_like: Set of unitary arrays only returned when compute_uv is set to True.

It raises a LinALgError when the singular values diverse.

Prerequisites for setup

Before we dive into the examples, make sure you have the numpy module installed in your local system. This is required for using linear algebraic functions like the one discussed in this article. Run the following command in your terminal.

pip install numpy

That’s all you need right now, let’s look at how we will implement the code in the next section.

To calculate Singular Value Decomposition (SVD) in Python, use the NumPy library’s linalg.svd() function. Its syntax is numpy.linalg.svd(A, full_matrices=True, compute_uv=True, hermitian=False), where A is the matrix for which SVD is being calculated. It returns three matrices: S, U, and V.

Example 1: Calculating the Singular Value Decomposition of a 3×3 Matrix

In this first example we will take a 3X3 matrix and compute its singular value decomposition in the following way:

#importing the numpy module
import numpy as np
#using the numpy.array() function to create an array
A=np.array([[2,4,6],
       [8,10,12],
       [14,16,18]])
#calculatin all three matrices for the output
#using the numpy linalg.svd function
u,s,v=np.linalg.svd(A, compute_uv=True)
#displaying the result
print("the output is=")
print('s(the singular value) = ',s)
print('u = ',u)
print('v = ',v)

The output will be:

the output is=
s(the singular value) =  [3.36962067e+01 2.13673903e+00 8.83684950e-16]
u =  [[-0.21483724  0.88723069  0.40824829]
 [-0.52058739  0.24964395 -0.81649658]
 [-0.82633754 -0.38794278  0.40824829]]
v =  [[-0.47967118 -0.57236779 -0.66506441]
 [-0.77669099 -0.07568647  0.62531805]
 [-0.40824829  0.81649658 -0.40824829]]
Example 1
Example 1

Example 2: Calculating the Singular Value Decomposition of a Random Matrix

In this example, we will be using the numpy.random.randint() function to create a random matrix. Let’s get into it!

#importing the numpy module
import numpy as np
#using the numpy.array() function to craete an array
A=np.random.randint(5, 200, size=(3,3))
#display the created matrix
print("The input matrix is=",A)
#calculatin all three matrices for the output
#using the numpy linalg.svd function
u,s,v=np.linalg.svd(A, compute_uv=True)
#displaying the result
print("the output is=")
print('s(the singular value) = ',s)
print('u = ',u)
print('v = ',v)

The output will be as follows:

The input matrix is= [[ 36  74 101]
 [104 129 185]
 [139 121 112]]
the output is=
s(the singular value) =  [348.32979681  61.03199722  10.12165841]
u =  [[-0.3635535  -0.48363012 -0.79619769]
 [-0.70916514 -0.41054007  0.57318554]
 [-0.60408084  0.77301925 -0.19372034]]
v =  [[-0.49036384 -0.54970618 -0.67628871]
 [ 0.77570499  0.0784348  -0.62620264]
 [ 0.39727203 -0.83166766  0.38794824]]
Example 2
Example 2

Suggested: Numpy linalg.eigvalsh: A Guide to Eigenvalue Computation.

Wrapping Up

In this article, we explored the concept of singular value decomposition in mathematics and how to calculate it using Python’s numpy module. We used the linalg.svd() function to compute the singular value decomposition of both given and random matrices. Numpy provides an efficient and easy-to-use method for performing linear algebra operations, making it highly valuable in machine learning, neural networks, and regression analysis. Keep exploring other linear algebraic functions in numpy to enhance your mathematical toolset in Python.

To know more about the linear algebraic functions in numpy, click here.