Bird
0
0

What will be the output of this code?

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

    Data 4 at (0,1), 5 at (1,2), 6 at (2,0) in a 3x3 matrix.
  2. Step 2: Construct full matrix

    Positions without data are zero, so matrix is [[0 4 0], [0 0 5], [6 0 0]].
  3. Final Answer:

    [[0 4 0] [0 0 5] [6 0 0]] -> Option A
  4. Quick Check:

    Data at (row,col) = matrix entries [OK]
Quick Trick: Match data with (row, col) indices to build matrix [OK]
Common Mistakes:
MISTAKES
  • Mixing row and column indices
  • Assuming data fills diagonal
  • Confusing CSR/CSC formats with COO

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes