Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
SciPy - Sparse Matrices (scipy.sparse)
What will be the output of this code?
from scipy.sparse import csr_matrix
import numpy as np
mat = csr_matrix(np.array([[0, 1], [2, 0]]))
print(mat.toarray())
A[[0 1] [2 0]]
B[[1 0] [0 2]]
C[[0 0] [0 0]]
D[[1 2] [0 0]]
Step-by-Step Solution
Solution:
  1. Step 1: Convert numpy array to CSR sparse matrix

    The matrix is [[0,1],[2,0]] converted to sparse format.
  2. Step 2: Use toarray() to get dense matrix back

    Calling toarray() returns the original dense matrix.
  3. Final Answer:

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

    toarray() returns original matrix [OK]
Quick Trick: toarray() converts sparse back to dense matrix [OK]
Common Mistakes:
MISTAKES
  • Mixing row and column values
  • Expecting sparse format output
  • Confusing toarray() with data attribute

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes