Bird
Raised Fist0
SciPydata~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1.

What does the svd function in scipy.linalg return when applied to a matrix?

easy
A. The determinant of the matrix
B. Only the singular values as a matrix
C. The inverse of the matrix
D. Three matrices: U, singular values as a vector, and VT

Solution

  1. Step 1: Understand the output of SVD

    The Singular Value Decomposition splits a matrix into three parts: U, singular values, and VT.
  2. Step 2: Recall scipy.linalg.svd output

    Scipy's svd returns U (left singular vectors), a vector of singular values, and VT (right singular vectors transposed).
  3. Final Answer:

    Three matrices: U, singular values as a vector, and VT -> Option D
  4. Quick Check:

    SVD output = U, s, VT [OK]
Hint: Remember SVD splits matrix into U, s, VT [OK]
Common Mistakes:
  • Thinking svd returns only singular values
  • Confusing VT with V
  • Expecting a single matrix output
2.

Which of the following is the correct way to import and use svd from scipy.linalg?

???
easy
A. import svd from scipy.linalg U, s, VT = svd(matrix)
B. import scipy.linalg.svd U, s, VT = scipy.linalg.svd(matrix)
C. from scipy.linalg import svd U, s, VT = svd(matrix)
D. from scipy import svd U, s, VT = svd(matrix)

Solution

  1. Step 1: Check correct import syntax

    Correct import for svd is from scipy.linalg import svd.
  2. Step 2: Confirm usage

    Then call svd(matrix) to get U, s, VT.
  3. Final Answer:

    from scipy.linalg import svd\nU, s, VT = svd(matrix) -> Option C
  4. Quick Check:

    Correct import and call = from scipy.linalg import svd U, s, VT = svd(matrix) [OK]
Hint: Use 'from scipy.linalg import svd' to import [OK]
Common Mistakes:
  • Using incorrect import syntax
  • Trying to import svd directly from scipy
  • Using 'import svd from' which is invalid
3.

Given the code below, what is the shape of U, s, and VT?

import numpy as np
from scipy.linalg import svd

matrix = np.array([[1, 2], [3, 4], [5, 6]])
U, s, VT = svd(matrix)
medium
A. U: (3, 3), s: (2,), VT: (2, 2)
B. U: (3, 2), s: (2,), VT: (2, 2)
C. U: (2, 2), s: (2,), VT: (3, 3)
D. U: (3, 3), s: (3,), VT: (2, 2)

Solution

  1. Step 1: Identify matrix shape

    The matrix is 3 rows by 2 columns (3x2).
  2. Step 2: Recall full SVD shapes

    Full SVD returns U as (m, m), s as (min(m,n),), VT as (n, n). Here m=3, n=2, so U=(3,3), s=(2,), VT=(2,2).
  3. Final Answer:

    U: (3, 3), s: (2,), VT: (2, 2) -> Option A
  4. Quick Check:

    Full SVD shapes = U(m,m), s(min(m,n)), VT(n,n) [OK]
Hint: Full SVD U shape = (m,m), VT = (n,n), s length = min(m,n) [OK]
Common Mistakes:
  • Assuming U shape is (m,n) instead of (m,m)
  • Confusing s shape with VT shape
  • Mixing up rows and columns in shapes
4.

What is wrong with the following code snippet that tries to compute the SVD of a matrix?

from scipy.linalg import svd

matrix = [[1, 0], [0, 1]]
U, s, VT = svd(matrix)
print(U, s, VT)
medium
A. svd function does not return three values
B. The matrix should be a NumPy array, not a list of lists
C. The import statement is incorrect
D. The print statement syntax is wrong

Solution

  1. Step 1: Check input type for svd

    svd expects a NumPy array or similar numeric array, not a plain Python list.
  2. Step 2: Identify error cause

    Passing a list of lists can cause unexpected behavior or errors; converting to np.array fixes this.
  3. Final Answer:

    The matrix should be a NumPy array, not a list of lists -> Option B
  4. Quick Check:

    Input must be np.array for svd [OK]
Hint: Always convert input to np.array before svd [OK]
Common Mistakes:
  • Passing Python lists directly to svd
  • Assuming svd returns a single matrix
  • Incorrect import or print syntax
5.

You have a large dataset matrix X of shape (1000, 50). You want to reduce its dimensionality to 10 while keeping most information. Which step using scipy.linalg.svd helps achieve this?

hard
A. Compute full SVD, then keep only the first 10 singular values and corresponding vectors
B. Compute SVD and discard the singular values
C. Use svd to get U and VT, then multiply all components without truncation
D. Apply svd only on a 10x10 submatrix of X

Solution

  1. Step 1: Understand dimensionality reduction with SVD

    Reducing dimensions means keeping top singular values and vectors that capture most variance.
  2. Step 2: Apply truncation after full SVD

    Compute full SVD, then keep first 10 singular values and corresponding columns of U and rows of VT to reduce size.
  3. Final Answer:

    Compute full SVD, then keep only the first 10 singular values and corresponding vectors -> Option A
  4. Quick Check:

    Truncate SVD components to reduce dimensions [OK]
Hint: Keep top singular values and vectors for reduction [OK]
Common Mistakes:
  • Discarding singular values loses information
  • Using only a submatrix loses global structure
  • Not truncating leads to no reduction
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard