w3resource

Understanding numpy.linalg.svd for Matrix Operations


numpy.linalg.svd in Python for Matrix Operations

numpy.linalg.svd performs Singular Value Decomposition (SVD), a fundamental matrix factorization technique in linear algebra. SVD decomposes a matrix into three components: a unitary matrix (U), a diagonal matrix of singular values (S), and another unitary matrix (Vh). This method is widely used in dimensionality reduction, machine learning, and numerical computations.


Syntax:

numpy.linalg.svd(a, full_matrices=True, compute_uv=True, hermitian=False)

Parameters:

    1. a (array_like):
    Input matrix to be decomposed.

    2. full_matrices (bool, optional):
    If True, produces full-sized U and Vh. Defaults to True.

    3. compute_uv (bool, optional):
    If True, computes both U and Vh along with S. If False, only S is computed. Defaults to True.

    4. hermitian (bool, optional):
    If True, assumes a is Hermitian. Optimizes computation for such matrices. Defaults to False.

Returns:

  • U (ndarray):
    Left singular vectors.
  • S (ndarray):
    Singular values in descending order.
  • Vh (ndarray):
    Right singular vectors (transposed).

Examples:

Example 1: Basic SVD Decomposition

Code:

import numpy as np

# Define a matrix
matrix = np.array([[1, 2], [3, 4], [5, 6]])

# Perform SVD
U, S, Vh = np.linalg.svd(matrix)

# Print results
print("U (Left singular vectors):\n", U)
print("Singular values:\n", S)
print("Vh (Right singular vectors):\n", Vh)

Output:

U (Left singular vectors):
 [[-0.2298477   0.88346102  0.40824829]
 [-0.52474482  0.24078249 -0.81649658]
 [-0.81964194 -0.40189603  0.40824829]]
Singular values:
 [9.52551809 0.51430058]
Vh (Right singular vectors):
 [[-0.61962948 -0.78489445]
 [-0.78489445  0.61962948]]

Explanation:

  • The input matrix is decomposed into U, S, and Vh.
  • S contains the singular values, and the matrices U and Vh are orthogonal.

Example 2: Reconstruct the Original Matrix

Code:


import numpy as np

# Define a matrix
matrix = np.array([[1, 2], [3, 4], [5, 6]])

# Perform SVD
U, S, Vh = np.linalg.svd(matrix)

# Reconstruct the matrix
S_matrix = np.zeros((U.shape[1], Vh.shape[0]))
np.fill_diagonal(S_matrix, S)
reconstructed_matrix = np.dot(U, np.dot(S_matrix, Vh))

# Print reconstructed matrix
print("Reconstructed matrix:\n", reconstructed_matrix)

Output:

Reconstructed matrix:
 [[1. 2.]
 [3. 4.]
 [5. 6.]]

Explanation:

By combining U, S, and Vh, the original matrix is reconstructed with high precision.


Example 3: SVD with Reduced Dimensions

Code:

import numpy as np

# Define a matrix
matrix = np.array([[1, 2], [3, 4], [5, 6]])

# Perform reduced SVD
U, S, Vh = np.linalg.svd(matrix, full_matrices=False)

# Print results
print("Reduced U:\n", U)
print("Singular values:\n", S)
print("Reduced Vh:\n", Vh)

Output:

Reduced U:
 [[-0.2298477   0.88346102]
 [-0.52474482  0.24078249]
 [-0.81964194 -0.40189603]]
Singular values:
 [9.52551809 0.51430058]
Reduced Vh:
 [[-0.61962948 -0.78489445]
 [-0.78489445  0.61962948]]

Explanation:

    With full_matrices=False, the dimensions of U and Vh are reduced, optimizing memory and computation for larger datasets.



Example 4: Singular Value Filtering for Noise Reduction

Code:

import numpy as np

# Define a noisy matrix
matrix = np.array([[1, 2], [3, 4], [5, 6]]) + np.random.normal(0, 0.1, (3, 2))

# Perform SVD
U, S, Vh = np.linalg.svd(matrix)

# Filter small singular values (e.g., below 0.5)
S_filtered = np.where(S > 0.5, S, 0)

# Reconstruct denoised matrix
S_matrix = np.zeros((U.shape[1], Vh.shape[0]))
np.fill_diagonal(S_matrix, S_filtered)
denoised_matrix = np.dot(U, np.dot(S_matrix, Vh))

print("Denoised matrix:\n", denoised_matrix)

Output:

Denoised matrix:
 [[1.27191825 1.60511088]
 [3.06257666 3.86485146]
 [4.88602421 6.16597066]]

Explanation:

Small singular values are set to zero, reducing noise in the reconstructed matrix.


Applications of SVD:

    1. Dimensionality Reduction:
    Principal Component Analysis (PCA) is built on SVD to reduce data dimensions while retaining most variance.

    2. Recommender Systems:
    SVD identifies latent features in user-item matrices.

    3. Noise Filtering:
    Removes less significant components to clean data.

    4. Image Compression:
    Reduces image size without significant loss of quality.

    5. Solving Linear Systems:
    Solves underdetermined or overdetermined systems.


Additional Notes:

    1. Complex Matrices:
    For Hermitian matrices, use hermitian=True for optimized computation.

    2. Numerical Stability:
    SVD is numerically stable and robust to small perturbations in input data.

    3. Runtime Complexity: The computational cost is O(m*n*min(m, n)) for an m x n matrix.

Practical Guides to NumPy Snippets and Examples.



Follow us on Facebook and Twitter for latest update.