Bird
0
0

What is wrong with this code?

medium📝 Debug Q7 of 15
SciPy - Sparse Matrices (scipy.sparse)
What is wrong with this code?
from scipy.sparse import csr_matrix
row = [0, 1]
col = [1, 2]
data = [3, 4]
sparse = csr_matrix((data, (row, col)))
print(sparse.shape)
Arow and col must be numpy arrays
Bcsr_matrix cannot be created from data and indices
CShape parameter is missing, so shape is ambiguous
DData values must be integers
Step-by-Step Solution
Solution:
  1. Step 1: Check csr_matrix constructor usage

    When shape is not provided, csr_matrix infers shape from max indices.
  2. Step 2: Understand shape inference

    Since col has index 2, shape should be at least (2, 3), but not explicitly set, so shape may be ambiguous.
  3. Final Answer:

    Shape parameter is missing, so shape is ambiguous -> Option C
  4. Quick Check:

    Missing shape can cause ambiguity [OK]
Quick Trick: Always specify shape to avoid ambiguity [OK]
Common Mistakes:
MISTAKES
  • Assuming shape is optional always
  • Thinking csr_matrix can't use data and indices
  • Believing data must be integers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes