Bird
0
0

What is wrong with this code?

medium📝 Debug Q7 of 15
SciPy - Sparse Linear Algebra
What is wrong with this code?
from scipy.sparse.linalg import svds
from scipy.sparse import csr_matrix
A = csr_matrix([[1, 0], [0, 1]])
U, s, Vt = svds(A, k=3)
Asvds requires k to be 1
Bcsr_matrix cannot be created from list of lists
Ck cannot be greater than min(A.shape)
DMissing numpy import
Step-by-Step Solution
Solution:
  1. Step 1: Check matrix shape and k value

    The matrix A is 2x2, so the maximum k allowed is min(2,2) - 1 = 1.
  2. Step 2: Understand svds k parameter limits

    svds requires k to be less than min(A.shape), so k=3 is invalid here.
  3. Final Answer:

    k cannot be greater than min(A.shape) -> Option C
  4. Quick Check:

    k must be less than min(matrix dimensions) [OK]
Quick Trick: k < min(matrix rows, cols) for svds [OK]
Common Mistakes:
  • Setting k larger than matrix smallest dimension
  • Assuming csr_matrix can't be created from lists
  • Ignoring svds k parameter constraints

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes