0
0
SciPydata~10 mins

Singular Value Decomposition (svd) in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Singular Value Decomposition (svd)
Start with matrix A
Compute U, S, V^T such that A = U * S * V^T
U: left singular vectors
S: singular values (diagonal matrix)
V^T: right singular vectors transposed
Use U, S, V^T for analysis or reconstruction
We start with a matrix A, then compute three matrices U, S, and V^T. These satisfy A = U * S * V^T. U and V^T contain special vectors, and S has singular values that tell us about A's structure.
Execution Sample
SciPy
import numpy as np
from scipy.linalg import svd
A = np.array([[3, 1], [1, 3]])
U, S, VT = svd(A)
print(U)
print(S)
print(VT)
This code computes the singular value decomposition of a 2x2 matrix A and prints the U, S, and V^T matrices.
Execution Table
StepActionMatrix/Vector ComputedValue/Description
1Input matrix AA[[3, 1], [1, 3]]
2Compute U, S, VT using svd(A)U[[-0.707, -0.707], [-0.707, 0.707]]
3Compute singular values SS[4.0, 2.0]
4Compute VT (transpose of V)VT[[-0.707, -0.707], [-0.707, 0.707]]
5Verify reconstruction A = U * diag(S) * VTReconstructed A[[3.0, 1.0], [1.0, 3.0]]
6EndSVD complete, matrices ready for use
💡 All matrices computed and verified; decomposition finished.
Variable Tracker
VariableStartAfter svdFinal
A[[3, 1], [1, 3]][[3, 1], [1, 3]][[3, 1], [1, 3]]
UNone[[-0.707, -0.707], [-0.707, 0.707]][[-0.707, -0.707], [-0.707, 0.707]]
SNone[4.0, 2.0][4.0, 2.0]
VTNone[[-0.707, -0.707], [-0.707, 0.707]][[-0.707, -0.707], [-0.707, 0.707]]
Key Moments - 3 Insights
Why are the singular values in S always non-negative?
Singular values represent lengths (magnitudes) of axes after transformation, so they cannot be negative. See execution_table step 3 where S contains positive values.
Why do U and VT matrices have orthogonal columns?
U and VT contain special vectors called singular vectors that are orthogonal (at right angles) to each other, ensuring the decomposition is stable. This is shown in execution_table steps 2 and 4.
How do we check that the decomposition is correct?
By multiplying U, the diagonal matrix of S, and VT, we get back the original matrix A, as shown in execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what are the singular values in S?
A[4.0, 2.0]
B[3.0, 1.0]
C[0.707, 0.707]
D[1.0, 3.0]
💡 Hint
Check the 'Matrix/Vector Computed' column at step 3 in execution_table.
At which step in the execution_table do we verify the original matrix is reconstructed?
AStep 3
BStep 2
CStep 5
DStep 6
💡 Hint
Look for the row mentioning 'Verify reconstruction' in execution_table.
If the input matrix A was changed to [[4, 0], [0, 1]], how would the singular values S likely change?
AThey would be [0, 0]
BThey would be [4, 1]
CThey would be negative
DThey would be the same as before
💡 Hint
Singular values reflect the scale of the matrix along axes; see variable_tracker for S values.
Concept Snapshot
Singular Value Decomposition (SVD):
- Decomposes matrix A into U, S, VT
- A = U * diag(S) * VT
- U and VT have orthogonal columns
- S contains non-negative singular values
- Used for data analysis, noise reduction, and more
Full Transcript
Singular Value Decomposition (SVD) breaks a matrix into three parts: U, S, and VT. U and VT contain special vectors that are orthogonal, and S contains singular values which are always non-negative. The product of U, S, and VT reconstructs the original matrix. This helps us understand the structure of data and is useful in many data science tasks. We showed step-by-step how SVD works on a simple 2x2 matrix, tracking variables and verifying the results.