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.
scipy.linalg.svd
FunctionThe 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:
The SVD of a matrix
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:
a
is a required parameter representing the input matrix for which SVD needs to be computed.
full_matrices
is an optional boolean parameter. If True
which is the default value, it returns the full-sized matrices False
, it returns the reduced form of
compute_uv
is an optional boolean parameter. If True
which is the default value, it computes both False
, it computes only the singular values
Note: Make sure you have the SciPy library installed. To learn more about the SciPy installation on your system, click here.
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)
Line 1–2: Firstly, we import the necessary modules. The numpy
module and scipy.linalg.svd
from SciPy to perform the SVD technique.
Line 5: Next, we define a rectangular matrix A
with dimensions 3x2 using numpy
.
Line 8: Then, we compute the SVD of matrix A
using the svd
function and store the resulting matrices U
, S
, and Vt
.
Line 10–15: Finally, we print the left singular vectors matrix
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,
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