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 coo_matrix
row = [0, 1]
col = [1, 2]
data = [3, 4, 5]
sparse = coo_matrix((data, (row, col)))
Acoo_matrix requires dense matrix input, not tuples
BLength of data does not match length of row and col arrays
Crow and col arrays must be numpy arrays, not lists
DMissing shape parameter in coo_matrix constructor
Step-by-Step Solution
Solution:
  1. Step 1: Check input lengths

    coo_matrix expects data, row, and col arrays to have the same length.
  2. Step 2: Compare lengths

    data has length 3, but row and col have length 2 each, causing mismatch.
  3. Final Answer:

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

    Lengths must be equal for COO format [OK]
Quick Trick: Data, row, col arrays must be same length [OK]
Common Mistakes:
MISTAKES
  • Assuming lists are invalid inputs
  • Forgetting to match lengths of data and indices

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes