Bird
0
0

Find the bug in this code:

medium📝 Debug Q7 of 15
SciPy - Sparse Matrices (scipy.sparse)
Find the bug in this code:
from scipy.sparse import csr_matrix
csr = csr_matrix(([1, 2], [0, 1], [0, 1]))
print(csr.shape)
ATypeError because data is not a numpy array
BAttributeError: csr_matrix has no attribute 'shape'
CValueError due to incorrect indptr length
DNo error, prints (2, 2)
Step-by-Step Solution
Solution:
  1. Step 1: Check indptr length

    indptr length must be number of rows + 1; here length=3 means 2 rows.
  2. Step 2: Check indptr values

    indptr = [0,1] length 2 is invalid; should be length 3 for 2 rows.
  3. Step 3: Identify error

    Incorrect indptr length causes ValueError on csr_matrix creation.
  4. Final Answer:

    ValueError due to incorrect indptr length -> Option C
  5. Quick Check:

    indptr length must be rows+1 [OK]
Quick Trick: indptr length must be rows + 1; mismatch causes ValueError [OK]
Common Mistakes:
MISTAKES
  • Using indptr length equal to number of rows
  • Ignoring indptr array length rules
  • Assuming csr_matrix creation always succeeds

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes