Challenge - 5 Problems
Sparse SVD Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember that U has the same number of rows as the original matrix and k columns.
✗ Incorrect
The svds function returns U with shape (m, k) where m is the number of rows in the input matrix and k is the number of singular values requested.
❓ data_output
intermediate1: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))
Attempts:
2 left
💡 Hint
The singular values vector length equals k, the number of singular values requested.
✗ Incorrect
The svds function returns exactly k singular values in the vector s.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check the allowed range for k relative to matrix dimensions.
✗ Incorrect
The svds function requires k to be less than or equal to min(matrix dimensions) - 1. Here, k=5 is too large for a 4x4 matrix.
🚀 Application
advanced2: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?
Attempts:
2 left
💡 Hint
Recall how to multiply matrices and diagonal matrices in numpy.
✗ Incorrect
The correct reconstruction is U times the diagonal matrix of singular values times Vt. Using @ operator and np.diag(s) is correct.
🧠 Conceptual
expert1: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?
Attempts:
2 left
💡 Hint
Think about computational cost and memory when working with large sparse data.
✗ Incorrect
svds is designed to compute a small number of singular values and vectors efficiently without converting the whole matrix to dense form, saving time and memory.