0
0
SciPydata~20 mins

Sparse SVD (svds) in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sparse SVD Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Sparse SVD on a Simple Sparse Matrix
What is the shape of the matrix U returned by svds when decomposing a 5x5 sparse matrix with k=2?
SciPy
from scipy.sparse import csr_matrix
from scipy.sparse.linalg import svds
import numpy as np

A = csr_matrix(np.array([[1, 0, 0, 0, 2],
                         [0, 0, 3, 0, 0],
                         [0, 4, 0, 0, 0],
                         [0, 0, 0, 5, 0],
                         [6, 0, 0, 0, 0]]))

U, s, Vt = svds(A, k=2)
print(U.shape)
A(5, 2)
B(2, 5)
C(2, 2)
D(5, 5)
Attempts:
2 left
💡 Hint
Remember that U has the same number of rows as the original matrix and k columns.
data_output
intermediate
1:30remaining
Singular Values from Sparse SVD
Given a sparse matrix A and svds(A, k=3), what is the length of the singular values vector s?
SciPy
from scipy.sparse import diags
from scipy.sparse.linalg import svds

A = diags([1, 2, 3, 4, 5])
U, s, Vt = svds(A, k=3)
print(len(s))
A1
B5
C3
D0
Attempts:
2 left
💡 Hint
The singular values vector length equals k, the number of singular values requested.
🔧 Debug
advanced
2:00remaining
Error Raised by svds with k Larger Than Matrix Dimensions
What error does the following code raise? from scipy.sparse import csr_matrix from scipy.sparse.linalg import svds import numpy as np A = csr_matrix(np.eye(4)) U, s, Vt = svds(A, k=5)
SciPy
from scipy.sparse import csr_matrix
from scipy.sparse.linalg import svds
import numpy as np

A = csr_matrix(np.eye(4))
U, s, Vt = svds(A, k=5)
AValueError: k must be smaller than or equal to min(A.shape) - 1
BTypeError: svds() got an unexpected keyword argument 'k'
CRuntimeError: svds did not converge
DNo error, returns U, s, Vt with shapes (4,5), (5,), (5,4)
Attempts:
2 left
💡 Hint
Check the allowed range for k relative to matrix dimensions.
🚀 Application
advanced
2:30remaining
Using svds to Approximate a Matrix
You have a large sparse matrix A of shape (1000, 1000). You want to approximate it using svds with k=10. Which of the following code snippets correctly reconstructs the approximate matrix?
SciPy
from scipy.sparse.linalg import svds

U, s, Vt = svds(A, k=10)
# Which code reconstructs the approximation?
Aapprox = U * s * Vt
Bapprox = U @ np.diag(s) @ Vt
Capprox = np.dot(U, s, Vt)
Dapprox = U + s + Vt
Attempts:
2 left
💡 Hint
Recall how to multiply matrices and diagonal matrices in numpy.
🧠 Conceptual
expert
1:30remaining
Why Use svds Instead of Full SVD on Sparse Matrices?
Which reason best explains why svds is preferred over full SVD for large sparse matrices?
Asvds can only be used on dense matrices, not sparse
Bsvds always produces more accurate singular values than full SVD
Csvds converts sparse matrices to dense internally for faster computation
Dsvds computes only a few singular values and vectors, saving time and memory
Attempts:
2 left
💡 Hint
Think about computational cost and memory when working with large sparse data.