Bird
0
0

What will be the output of the following code?

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

arr = np.array([[0, 1], [2, 0]])
sparse_mat = csr_matrix(arr)
result = sparse_mat.dot(np.array([1, 2]))
print(result.toarray())
A[[2] [2]]
B[[2 4] [1 2]]
C[[2 0] [0 1]]
D[[2] [1]]
Step-by-Step Solution
Solution:
  1. Step 1: Understand matrix and vector multiplication

    The sparse matrix is [[0,1],[2,0]]. Multiplying by vector [1,2] means:
    Row 1: 0*1 + 1*2 = 2
    Row 2: 2*1 + 0*2 = 2
  2. Step 2: Check the printed output

    The result is a 2x1 matrix with values [[2],[2]]. But the code prints result.toarray(), which shows a 2D array.
    So output is:
    [[2]
    [2]]
  3. Final Answer:

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

    Dot product result = [[2],[2]] [OK]
Quick Trick: Multiply rows by vector elements and sum for dot product [OK]
Common Mistakes:
MISTAKES
  • Mixing element-wise and dot product multiplication
  • Confusing shape of output array
  • Incorrectly summing elements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes