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
import numpy as np
row = np.array([0, 1, 2])
col = np.array([0, 1])
data = np.array([1, 2, 3])
sparse = csr_matrix((data, (row, col)))
AMissing shape parameter causes error
Bcsr_matrix requires dense arrays, not sparse
Cdata array must be integers only
Drow and col arrays have different lengths than data array
Step-by-Step Solution
Solution:
  1. Step 1: Check lengths of arrays

    row has length 3, col has length 2, data has length 3; lengths must match.
  2. Step 2: Identify mismatch

    Mismatch between col length and data length causes error.
  3. Final Answer:

    row and col arrays have different lengths than data array -> Option D
  4. Quick Check:

    Indices and data lengths must match [OK]
Quick Trick: Ensure row, col, and data arrays have equal lengths [OK]
Common Mistakes:
MISTAKES
  • Ignoring length mismatch between indices and data
  • Assuming shape parameter is mandatory

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes