Overview
- The function svd() from the linalg module of NumPy library calculates the singular values of an input array A and returns the results contained in an SVDResult instance.
- The first ndarray in the SVDResult instance contains the left singular vector.
- The second ndarray in the SVDResult instance contains the singular values of the given array A.
- The third ndarray in the SVDResult instance contains the right singular vector.
Singular Value Decomposition
- Using singular value decomposition the matrix A of size mxn is factorized into
- An orthogonal matrix U of dimension mxm.
- A complex conjugate(also known as Hermitian transpose) of orthogonal matrix V of dimension nxn
- A diagonal matrix Σ of dimension mxn, containing singular values on its diagonal
as given by
A = UΣV*
- Unlike eigenvalues which are defined only for square matrices, singular values can be found for both square matrices and rectangular matrices.
- The singular values are non-negative real numbers.
- V*- Complex conjugate of V.
Note:
Orthogonal matrix: An orthogonal matrix is a matrix when multiplied by its transpose results in an identity matrix.
Identity matrix: An identity matrix is a matrix whose diagonal elements are with the value one. An identity matrix is a diagonal matrix.
Diagonal matrix: A diagonal matrix is a matrix whose elements are all ones or zeroes on its diagonal.
Example
# Example Python program that obtains the singular values of
# a square matrix
import numpy
# Create a 2x2 square matrix
a1 = numpy.ndarray(shape = (2, 2),
dtype = numpy.int32)
a1[0][0] = 2
a1[0][1] = 5
a1[1][0] = -7
a1[1][1] = -3
# Obtain the singular values of the matrix
svdResults = numpy.linalg.svd(a1)
print(type(svdResults))
print(svdResults)
print("Left singular vector:")
print(numpy.linalg.svd(a1)[0])
print("Right singular vector:")
print(numpy.linalg.svd(a1)[2])
print("Singular values of the matrix:")
print(numpy.linalg.svd(a1)[1])Output
<class 'numpy.linalg._linalg.SVDResult'>
SVDResult(U=array([[-0.52573111, 0.85065081],
[ 0.85065081, 0.52573111]]), S=array([8.71337969, 3.32821489]), Vh=array([[-0.80405286, -0.59455781],
[-0.59455781, 0.80405286]]))
Left singular vector:
[[-0.52573111 0.85065081]
[ 0.85065081 0.52573111]]
Right singular vector:
[[-0.80405286 -0.59455781]
[-0.59455781 0.80405286]]
Singular values of the matrix:
[8.71337969 3.32821489]Applications of Singular Value Decomposition
- Image processing
- Text analysis