Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
SciPy - Sparse Matrices (scipy.sparse)
Identify the error in this code snippet:
from scipy.sparse import csr_matrix
row = [0, 1]
col = [1, 2]
data = [3, 4]
sparse = csr_matrix(data, (row, col))
AThe data and indices must be passed as a single tuple
Brow and col should be numpy arrays, not lists
Ccsr_matrix cannot be created from data and indices
DThe shape parameter is missing
Step-by-Step Solution
Solution:
  1. Step 1: Check csr_matrix constructor usage

    The constructor expects a single tuple (data, (row, col)) as the first argument.
  2. Step 2: Analyze given code

    Here, data and (row, col) are passed as separate arguments, causing an error.
  3. Final Answer:

    The data and indices must be passed as a single tuple -> Option A
  4. Quick Check:

    Constructor argument format = single tuple [OK]
Quick Trick: Pass data and indices together as one tuple to csr_matrix [OK]
Common Mistakes:
MISTAKES
  • Passing data and indices as separate arguments
  • Assuming lists are invalid instead of arrays

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes