Bird
0
0

What will be the output of this code snippet?

medium📝 Predict Output Q5 of 15
SciPy - Sparse Matrices (scipy.sparse)
What will be the output of this code snippet?
from scipy.sparse import coo_matrix
row = [0, 0, 1]
col = [0, 2, 1]
data = [1, 2, 3]
matrix = coo_matrix((data, (row, col)), shape=(2, 3))
print(matrix.toarray())
A[[1 2 0] [0 0 3]]
B[[1 0 2] [0 0 3]]
C[[1 0 2] [0 3 0]]
D[[1 0 0] [0 2 3]]
Step-by-Step Solution
Solution:
  1. Step 1: Map data to matrix positions

    Data 1 at (0,0), 2 at (0,2), 3 at (1,1) in a 2x3 matrix.
  2. Step 2: Construct dense array

    Row 0: positions 0 and 2 filled with 1 and 2, position 1 zero. Row 1: position 1 filled with 3, others zero.
  3. Final Answer:

    [[1 0 2] [0 3 0]] -> Option C
  4. Quick Check:

    Data placed by (row, col) indices in array [OK]
Quick Trick: toarray() converts sparse to full matrix [OK]
Common Mistakes:
MISTAKES
  • Mixing row and column indices
  • Misplacing data values in output
  • Ignoring zeros in sparse matrix

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes