Bird
0
0

Identify the issue in this code snippet:

medium📝 Debug Q7 of 15
SciPy - Sparse Matrices (scipy.sparse)
Identify the issue in this code snippet:
from scipy.sparse import coo_matrix
row = [0, 1, 2]
col = [0, 1]
data = [1, 2, 3]
matrix = coo_matrix((data, (row, col)))
ALength of 'col' does not match length of 'data' and 'row'
BMissing shape parameter in coo_matrix()
CData values contain invalid types
DRow indices are out of bounds
Step-by-Step Solution
Solution:
  1. Step 1: Check array lengths

    In COO format, data, row, and col arrays must be the same length.
  2. Step 2: Identify mismatch

    Here, 'col' has length 2, but 'row' and 'data' have length 3, causing an error.
  3. Final Answer:

    Length of 'col' does not match length of 'data' and 'row' -> Option A
  4. Quick Check:

    All input arrays must have equal length [OK]
Quick Trick: Check that data, row, col arrays have equal length [OK]
Common Mistakes:
MISTAKES
  • Ignoring length mismatch between arrays
  • Assuming shape parameter is mandatory
  • Not verifying input array types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes