0
0
SciPydata~20 mins

Singular Value Decomposition (svd) in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SVD Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of SVD on a simple matrix
What is the shape of the matrices U, S, and VT after applying SVD to a 3x2 matrix using scipy.linalg.svd with full_matrices=False?
SciPy
import numpy as np
from scipy.linalg import svd

A = np.array([[1, 2], [3, 4], [5, 6]])
U, S, VT = svd(A, full_matrices=False)
print(U.shape, S.shape, VT.shape)
A(3, 2) (2,) (2, 2)
B(3, 3) (3,) (3, 2)
C(3, 2) (3,) (2, 2)
D(3, 3) (2,) (2, 2)
Attempts:
2 left
💡 Hint
Remember that full_matrices=False returns reduced shapes for U and VT.
data_output
intermediate
2:00remaining
Reconstruct matrix from SVD components
Given U, S, and VT from SVD of matrix A, which option correctly reconstructs A?
SciPy
import numpy as np
from scipy.linalg import svd

A = np.array([[7, 8], [9, 10], [11, 12]])
U, S, VT = svd(A, full_matrices=False)

# Which code reconstructs A correctly?
Anp.dot(U, S) @ VT
BU * S * VT
CU @ np.diag(S) @ VT
Dnp.diag(U) @ S @ VT
Attempts:
2 left
💡 Hint
Recall that S is a 1D array of singular values, so you need to convert it to a diagonal matrix.
visualization
advanced
2:00remaining
Visualizing singular values
Which code snippet correctly plots the singular values of matrix A using matplotlib?
SciPy
import numpy as np
from scipy.linalg import svd
import matplotlib.pyplot as plt

A = np.array([[3, 1, 1], [-1, 3, 1]])
U, S, VT = svd(A)

# Which code plots singular values S as a bar chart?
Aplt.bar(range(len(S)), S); plt.show()
Bplt.plot(S); plt.show()
Cplt.scatter(range(len(S)), S); plt.show()
Dplt.hist(S); plt.show()
Attempts:
2 left
💡 Hint
Bar charts are good for showing discrete values like singular values.
🧠 Conceptual
advanced
2:00remaining
Effect of zero singular values
If a matrix has singular values [5, 3, 0, 0], what does the presence of zero singular values indicate about the matrix?
AThe matrix is invertible
BThe matrix is full rank with rank 4
CThe matrix is symmetric
DThe matrix is rank deficient and has rank 2
Attempts:
2 left
💡 Hint
Zero singular values mean some dimensions collapse to zero.
🔧 Debug
expert
2:00remaining
Identify error in SVD reconstruction code
What error will this code produce when trying to reconstruct matrix A from its SVD components?
SciPy
import numpy as np
from scipy.linalg import svd

A = np.array([[1, 0], [0, 1]])
U, S, VT = svd(A)
A_reconstructed = U @ S @ VT
print(A_reconstructed)
ATypeError: unsupported operand type(s) for @: 'numpy.ndarray' and 'numpy.ndarray'
BValueError: shapes (2,2) and (2,) not aligned for matrix multiplication
CNo error, prints the original matrix
DNameError: name 'S' is not defined
Attempts:
2 left
💡 Hint
Check the shape and type of S before matrix multiplication.