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: UU, SS, and VTV^T.

  • UU 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.

  • SS 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.

  • VTV^T 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 AA with dimensions m×nm \times n is represented as:

Syntax

The syntax of the scipy.linalg.svd function is given below:

scipy.linalg.svd(a, full_matrices=True, compute_uv=True)
Syntax of scipy.linalg.svd() function

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 UU, SS, and VTV^T. If False, it returns the reduced form of UU and VTV^T matrices, depending on the input matrix's size.

  • compute_uv is an optional boolean parameter. If True which is the default value, it computes both UU and VTV^T. If False, it computes only the singular values SS and does not compute UU and VTV^T.

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 np
from scipy.linalg import svd
# Define a rectangular matrix
A = np.array([[1, 2], [3, 4], [5, 6]])
# Compute the singular value decomposition (SVD) of A
U, 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 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 UU, SS, and VTV^T in the variables U, S, and Vt.

  • Line 10–15: Finally, we print the left singular vectors matrix UU, singular values matrix SS, and right singular vectors matrix VTV^T 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 AA.

The output looks something like this:

As you can see in the output, UU is a 3×33\times3 matrix, and each column represents a left singular vector and VV is a 2×22\times2 matrix, and each column represents a right singular vector. SS is a 1×21\times2 vector, and it contains the singular values in decreasing order.

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

Copyright ©2025 Educative, Inc. All rights reserved