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 csr_matrix

arr = np.array([[0, 0, 3], [4, 0, 0]])
sparse_mat = csr_matrix(arr)
print(sparse_mat.data)
A[0 0 3 4 0 0]
B[0 3 4]
C[3 4 0]
D[3 4]
Step-by-Step Solution
Solution:
  1. Step 1: Understand csr_matrix data attribute

    The 'data' attribute stores only the non-zero elements of the sparse matrix in row-major order.
  2. Step 2: Identify non-zero elements in the array

    In arr, non-zero elements are 3 (at position [0,2]) and 4 (at position [1,0]).
  3. Final Answer:

    [3 4] -> Option D
  4. Quick Check:

    csr_matrix.data shows only non-zero values [OK]
Quick Trick: csr_matrix.data shows only non-zero values [OK]
Common Mistakes:
MISTAKES
  • Expecting zeros to appear in sparse data
  • Confusing data attribute with full matrix
  • Misreading the order of stored elements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes