The SVD of a matrix can be calculated by using the svd function within scipy.linalg.
Let us understand how to code up using the svd function using the following example:
- Initialize an array:
a = np.array([[1, 2], [3, 4]])
- Import the relevant packages and functions within the package:
from scipy import linalg
- Use the svd function to calculate the SVD matrices (U, Σ and V):
linalg.svd(a)
- The output of the preceding function is as follows:
(array([[-0.40455358, -0.9145143 ],
[-0.9145143 , 0.40455358]]),
array([ 5.4649857 , 0.36596619]),
array([[-0.57604844, -0.81741556],
[ 0.81741556, -0.57604844]]))
In order to further understand if the output is as per expectation, let us check if the multiplication of the individual matrices in the output results in a matrix that is very similar to our original matrix.
In this scenario, our U matrix is as follows:
([[-0.40455358,-0.9145143],[-0.9145143,0.40455358]])
The Σ matrix is as follows:
([ 5.4649857 , 0.36596619])
However, note that the preceding second output of the svd function is only the diagonal elements within the matrix (where every non-diagonal element is 0). Hence, the Σ matrix translates to the following:
np.array([[ 5.4649857 , 0],
[0,0.36596619]])
Note that the diagonal values are populated by the elements of the svd output we saw earlier.
The V matrix is as follows:
([[-0.57604844, -0.81741556], [ 0.81741556, -0.57604844]])
In order to cross-check and verify the svd function's output, we will multiply the U, Σ, and V matrices that we obtained earlier.
The output of the matrix multiplication should be as close to the original matrix as possible.
- Initialize the matrices:
U = np.array([[-0.40455358, -0.9145143 ],
[-0.9145143 , 0.40455358]])
sigma = np.array([[ 5.4649857 , 0],
[0,0.36596619]])
V=np.array([[-0.57604844, -0.81741556],
[ 0.81741556, -0.57604844]])
- Perform the matrix multiplication of U, sigma, and V matrices:
np.dot(np.dot(U,sigma),V)
- The output of the preceding calculation is as follows:
array([[ 0.99999999, 1.99999998],
[ 3.00000003, 4.00000001]])
Note that, the output matrix is almost the same as the original matrix.