Compute the factor of a given array by Singular Value Decomposition using NumPy


Singular Value Decomposition (SVD) is the matrix factorization technique which divides the matrix into three parts namely left singular matrix, a diagonal singular matrix and right singular matrix.

SVD is powerful tool used in linear algebra and it has number of applications in data analysis, machine Learning and signal processing. This is mainly used to compute the rank of the matrix, as well as to perform the linear equations and performing the image compression and many more operations.

Calculating Singular Value Decomposition

If we compose a real or complex matrix A with the size m x n, then the singular value decomposition computes the following factorization.

A = U * S * V ^T

Where

  • U is the orthogonal matrix with size m x m contains the left singular vector of A.

  • S is the diagonal matrix with the size m x n contains the singular values of A.

  • V^T is the orthogonal matrix with the size n x n contains the right singular vector of A.

The singular vectors U and V^T are the orthonormal i.e. they have the unit length and are perpendicular to each other. The singular values are arranged along the diagonal of S (diagonal matrix) in the descending order.

Singular Value Decomposition in Numpy

Numpy library in python, provides the linalg module. This module provides many functions, one among them is SVD() is used to calculate the Singular value decomposition of a given matrix.

Syntax

Following is the syntax used to calculate the singular value decomposition of the given matrix.

import numpy as np
np.linalg.svd(matrix)

Where,

  • numpy is the name of the library.

  • np is the alias name of the numpy.

  • linalg is the module.

  • svd is the function used to calculate the singular value decomposition.

  • matrix is the input matrix.

Example

When we want to calculate the Singular Value Decomposition(SVD), we have to pass the matrix as the input argument to the svd() function.

import numpy as np
matrix = np.arange(4,8).reshape(2,2)
singular_v_d = np.linalg.svd(matrix)
print("The singular value decomposition of the given 2x2 square matrix:",singular_v_d)

Output

When we run the above code, following output will be displayed, we can observe three arrays, first array is the left singular array, second is the diagonal singular values and last array is the right singular matrix.

The singular value decomposition of the given 2x2 square matrix: (array([[-0.57035846, -0.8213959 ],
   [-0.8213959 , 0.57035846]]), array([11.22355763, 0.17819662]), array([[-0.64238181, -0.76638477],
   [ 0.76638477, -0.64238181]]))

Example

Let’s see another example to work with the svd() function of the linalg module for calculating the singular value decomposition.

import numpy as np
matrix = np.array([[1,23,4],[5,34,56]])
singular_v_d = np.linalg.svd(matrix)
print("The singular value decomposition of the given matrix:",singular_v_d)

Output

The singular value decomposition of the given 2x2 square matrix: (array([[-0.24361576, 0.96987183],
   [-0.96987183, -0.24361576]]), array([67.60877519, 17.08957337]), array([[-0.07533009, -0.5706183 , -0.8177531 ],
   [-0.01452389, 0.82062411, -0.57128375],
   [-0.99705287, 0.0311579 , 0.07010528]]))

Example

Let’s see another example to work with the svd() function for calculating the singular value decomposition of the given matrix.

import numpy as np
matrix = np.array([[[12,34,23],[23,54,34]],[[10,23,24],[56,68,34]]])
singular_v_d = np.linalg.svd(matrix)
print("The singular value decomposition of the given matrix:",singular_v_d)

Output

The singular value decomposition of the given matrix: (array([[[-0.53294435, -0.84615029],
        [-0.84615029,  0.53294435]],

       [[-0.32001136, -0.94741371],
        [-0.94741371,  0.32001136]]]), array([[80.14845114,  2.49515114],
       [99.54423363, 14.55834985]]), array([[[-0.32261121, -0.79617538, -0.5118855 ],
        [ 0.84320202,  0.0039616 , -0.53758224],
        [ 0.43003763, -0.60505294,  0.67005863]],

       [[-0.56512848, -0.7211306 , -0.40074987],
        [ 0.58018245, -0.00204301, -0.81448398],
        [ 0.58653059, -0.69279613,  0.41954188]]]))

Updated on: 07-Aug-2023

82 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements