Bird
0
0

Given the following code, what will be the output of print(csr.data)?

medium📝 Predict Output Q13 of 15
SciPy - Sparse Matrices (scipy.sparse)
Given the following code, what will be the output of print(csr.data)?
import numpy as np
from scipy.sparse import csr_matrix

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

    The data attribute of a CSR matrix stores only the non-zero values in row-major order.
  2. Step 2: Identify non-zero elements in arr

    The non-zero elements are 1 (row 0, col 2), 2 (row 1, col 0), and 3 (row 2, col 1). So csr.data will be [1, 2, 3].
  3. Final Answer:

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

    csr.data = non-zero values list [OK]
Quick Trick: csr.data holds only non-zero values in order [OK]
Common Mistakes:
MISTAKES
  • Expecting csr.data to include zeros
  • Confusing csr.data with the full dense array
  • Misordering the non-zero elements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes