Bird
0
0

What will be printed by this code?

medium📝 Predict Output Q5 of 15
SciPy - Sparse Matrices (scipy.sparse)
What will be printed by this code?
import numpy as np
from scipy.sparse import csr_matrix

arr = np.zeros((3,3))
arr[0,1] = 7
arr[2,2] = 4
sparse_mat = csr_matrix(arr)
print(sparse_mat.data)
A[0. 0. 0.]
B[0. 7. 4.]
C[7. 0. 4.]
D[7. 4.]
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the array

    The array has zeros except at positions (0,1) with 7 and (2,2) with 4.
  2. Step 2: csr_matrix stores only non-zero data

    The sparse matrix's data attribute contains only non-zero values in row-major order.
  3. Step 3: Output

    Thus, sparse_mat.data will be [7. 4.]
  4. Final Answer:

    [7. 4.] -> Option D
  5. Quick Check:

    Only non-zero values printed [OK]
Quick Trick: csr_matrix.data shows only non-zero elements [OK]
Common Mistakes:
MISTAKES
  • Expecting zeros in data array
  • Confusing data with full matrix
  • Assuming data includes all elements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes