Singular Value Decomposition aka SVD is one of many matrix decomposition Technique that decomposes a matrix into 3 sub-matrices namely U, S, V where U is the left eigenvector, S is a diagonal matrix of singular values and V is called the right eigenvector. We can reconstruct SVD of an image by using linalg.svd() method of NumPy module.
linalg.svd(matrix, full_matrices=True, compute_uv=True, hermitian=False)
- matrix : A real or complex matrix of size > 2.
- full_matrices: If True the size of u and v matrics are m x n , if False then the shape of u and v matrices are m x k , where k is non-zero values only.
- compute_uv: Takes in boolean value to compute u and v matrices along with s matrix.
- hermitian: By default matrix is assumed to be Hermitian if it contains real-values, this is used internally for efficiently computing the singular values.
u.shape:(3648, 3648),s.shape:(3648,),v.shape:(3648, 5472)
The above output shape indicates that there are 3648 linearly independent eigenvectors in this image.
Now let us look at the variance of the image used over a singular vector graphically:
Explanation: The Variance Explained Graph above clearly shows that about 99.77 % of information is explained by the first eigenvector and its corresponding eigenvalues themselves. Therefore, it very much advisable to reconstruct the image with just the top few eigenvectors themselves.
In the below program based on the above discussion, we reconstruct the image using SVD:
- Though the 1st eigenvector contains 99.77% of information reconstructing an image solely from it does not give a clear picture.
- Using the top 15 vectors for the image reconstruction gives a good enough approximation. Also out of 3648 vectors which is a massive decrease in computation and also it compresses the image.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course