Bird
Raised Fist0
SciPydata~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of using svds from scipy.sparse.linalg in data science?
easy
A. To efficiently compute singular value decomposition on large sparse matrices
B. To perform dense matrix multiplication
C. To sort data in ascending order
D. To calculate the determinant of a matrix

Solution

  1. Step 1: Understand the function purpose

    svds is designed for sparse matrices, which are mostly empty, to find singular values and vectors efficiently.
  2. Step 2: Compare options with function use

    Options A, B, and C describe unrelated matrix operations. Only To efficiently compute singular value decomposition on large sparse matrices matches the purpose of svds.
  3. Final Answer:

    To efficiently compute singular value decomposition on large sparse matrices -> Option A
  4. Quick Check:

    svds = sparse SVD computation [OK]
Hint: Remember svds is for sparse matrices, not dense operations [OK]
Common Mistakes:
  • Confusing svds with dense SVD functions
  • Thinking svds sorts or multiplies matrices
  • Assuming svds calculates determinants
2. Which of the following is the correct way to import the svds function from SciPy?
easy
A. from scipy.sparse.linalg import svds
B. import svds from scipy.linalg
C. from scipy.linalg import svds
D. import svds from scipy.sparse

Solution

  1. Step 1: Identify the correct module for svds

    The svds function is part of scipy.sparse.linalg, which handles sparse linear algebra.
  2. Step 2: Check import syntax

    Python import syntax requires 'from module import function'. from scipy.sparse.linalg import svds matches this correctly.
  3. Final Answer:

    from scipy.sparse.linalg import svds -> Option A
  4. Quick Check:

    Correct import syntax = from scipy.sparse.linalg import svds [OK]
Hint: Use 'from scipy.sparse.linalg import svds' to import correctly [OK]
Common Mistakes:
  • Using wrong module like scipy.linalg instead of sparse.linalg
  • Incorrect import syntax like 'import svds from ...'
  • Importing from scipy.sparse which lacks svds
3. Given the following code, what will be the shape of the matrix U returned by svds?
import numpy as np
from scipy.sparse.linalg import svds
from scipy.sparse import csr_matrix

A = csr_matrix(np.array([[1, 0, 0], [0, 2, 0], [0, 0, 3]]))
U, S, Vt = svds(A, k=2)
medium
A. (3, 3)
B. (3, 2)
C. (2, 3)
D. (2, 2)

Solution

  1. Step 1: Understand svds output shapes

    For an input matrix of shape (m, n) and parameter k, svds returns U with shape (m, k), S with length k, and Vt with shape (k, n).
  2. Step 2: Apply to given matrix

    Matrix A is 3x3, k=2, so U shape is (3, 2).
  3. Final Answer:

    (3, 2) -> Option B
  4. Quick Check:

    U shape = (rows, k) = (3, 2) [OK]
Hint: U shape is (rows, k) where k is number of singular values [OK]
Common Mistakes:
  • Confusing U shape with Vt shape
  • Assuming U is square matrix
  • Mixing up k with matrix dimensions
4. What is wrong with the following code snippet that tries to compute sparse SVD?
from scipy.sparse.linalg import svds
import numpy as np

A = np.array([[1, 0], [0, 1]])
U, S, Vt = svds(A, k=1)
medium
A. svds does not return three outputs
B. Parameter k cannot be 1
C. Matrix A is not a sparse matrix
D. Import statement is incorrect

Solution

  1. Step 1: Check matrix type requirement

    svds expects a sparse matrix input, but A is a dense numpy array.
  2. Step 2: Validate other parts

    Parameter k=1 is valid, svds returns three outputs, and import is correct. So only matrix type is wrong.
  3. Final Answer:

    Matrix A is not a sparse matrix -> Option C
  4. Quick Check:

    Input must be sparse matrix [OK]
Hint: Convert dense arrays to sparse before svds [OK]
Common Mistakes:
  • Passing dense numpy arrays directly to svds
  • Thinking k=1 is invalid
  • Misunderstanding svds output count
5. You have a large sparse user-item rating matrix with shape (10000, 5000). You want to reduce its dimensionality to 50 features using svds. Which of the following code snippets correctly performs this and returns the reduced user features matrix?
hard
A. from scipy.sparse.linalg import svds U, S, Vt = svds(ratings_sparse, k=50) user_features = np.diag(S) @ Vt
B. from scipy.sparse.linalg import svds U, S, Vt = svds(ratings_sparse, k=50) user_features = Vt.T @ np.diag(S)
C. from scipy.linalg import svd U, S, Vt = svd(ratings_sparse) user_features = U[:, :50]
D. from scipy.sparse.linalg import svds U, S, Vt = svds(ratings_sparse, k=50) user_features = U @ np.diag(S)

Solution

  1. Step 1: Understand svds output and dimensionality reduction

    svds returns U (users x k), S (k,), and Vt (k x items). Multiplying U by diag(S) gives user features in reduced space.
  2. Step 2: Analyze options for correct user features

    from scipy.sparse.linalg import svds U, S, Vt = svds(ratings_sparse, k=50) user_features = U @ np.diag(S) correctly computes user_features = U @ diag(S). from scipy.sparse.linalg import svds U, S, Vt = svds(ratings_sparse, k=50) user_features = np.diag(S) @ Vt mixes user and item matrices. from scipy.linalg import svd U, S, Vt = svd(ratings_sparse) user_features = U[:, :50] uses dense svd, not sparse. from scipy.sparse.linalg import svds U, S, Vt = svds(ratings_sparse, k=50) user_features = Vt.T @ np.diag(S) computes item features, not user features.
  3. Final Answer:

    from scipy.sparse.linalg import svds U, S, Vt = svds(ratings_sparse, k=50) user_features = U @ np.diag(S) -> Option D
  4. Quick Check:

    User features = U * S diagonal [OK]
Hint: Multiply U by diag(S) for user features after svds [OK]
Common Mistakes:
  • Using Vt for user features instead of U
  • Using dense svd on sparse data
  • Not multiplying U by singular values