What is scipy.linalg.svd() in Python?
SciPy is a strong Python-based module that offers a variety of scientific computing features. It is an extension of the NumPy library, providing functions to perform computing tasks efficiently.
The scipy.linalg module is primarily concerned with linear algebra operations. It is an essential tool for solving systems of linear equations and transformations, and it has applications in a wide range of domains, including machine learning.
The scipy.linalg.svd Function
The scipy.linalg.svd function is used to compute the Singular Value Decomposition (SVD) of a given matrix A.
SVD is a matrix factorization technique that decomposes a matrix into three separate matrices:
is the left singular vector matrix. It has orthogonal vectors that span the column space and gives information about how the columns of matrix A are related to each other. is the singular value matrix. It has singular values that are the diagonal entries of the diagonal matrix representing the magnitudes of the singular vectors. is the conjugate transpose of the right singular vector matrix. It has orthogonal vectors that span the row space and gives information about how the rows of matrix A are related to each other.
The SVD of a matrix
Syntax
The syntax of the scipy.linalg.svd function is given below:
scipy.linalg.svd(a, full_matrices=True, compute_uv=True)
Some of the parameters are discussed below:
ais a required parameter representing the input matrix for which SVD needs to be computed.full_matricesis an optional boolean parameter. IfTruewhich is the default value, it returns the full-sized matrices, , and . If False, it returns the reduced form ofand matrices, depending on the input matrix's size. compute_uvis an optional boolean parameter. IfTruewhich is the default value, it computes bothand . If False, it computes only the singular valuesand does not compute and .
Note: Make sure you have the SciPy library installed. To learn more about the SciPy installation on your system, click here.
Code
Let's implement the function scipy.linalg.inv() in the given sample code:
import numpy as npfrom scipy.linalg import svd# Define a rectangular matrixA = np.array([[1, 2], [3, 4], [5, 6]])# Compute the singular value decomposition (SVD) of AU, s, V = svd(A)print("Left Singular Vectors (U):")print(U)print("Singular Values (s):")print(s)print("Right Singular Vectors (V):")print(V)
Code explanation
Line 1–2: Firstly, we import the necessary modules. The
numpymodule andscipy.linalg.svdfrom SciPy to perform the SVD technique.Line 5: Next, we define a rectangular matrix
Awith dimensions 3x2 usingnumpy.Line 8: Then, we compute the SVD of matrix
Ausing thesvdfunction and store the resulting matrices, , and in the variables U,S, andVt.Line 10–15: Finally, we print the left singular vectors matrix
, singular values matrix , and right singular vectors matrix on the console.
Output
Upon execution, the code will use the function scipy.linalg.svd() and compute the Singular Value Decomposition of the rectangular matrix
The output looks something like this:
As you can see in the output,
Conclusion
Hence, the scipy.linalg.svd() function in Python is a powerful tool for computing a matrix's Singular Value Decomposition. The use of SVD is essential in a variety of scientific and technical applications such as data compression, linear equation solving, and more. The linear algebra module in SciPy has functionalities, including the svd() method, which make it an ideal library for handling complex scientific computations.
Free Resources