Bird
0
0

Identify the error in the following code snippet that attempts to create a CSR matrix:

medium📝 Debug Q14 of 15
SciPy - Sparse Matrices (scipy.sparse)
Identify the error in the following code snippet that attempts to create a CSR matrix:
from scipy.sparse import csr_matrix

arr = [[0, 1], [2, 0]]
csr = csr_matrix(arr, shape=(2, 1))
print(csr.shape)
AThe shape parameter is incompatible with the input array size.
BThe input array must be a NumPy array, not a list.
Ccsr_matrix does not accept the shape parameter.
DThe print statement syntax is incorrect.
Step-by-Step Solution
Solution:
  1. Step 1: Check input array and shape parameter

    The input array has shape (2, 2), but the shape parameter is set to (2, 1), which conflicts.
  2. Step 2: Understand csr_matrix shape rules

    The shape parameter must match or be compatible with the input data shape. Here, (2, 1) is incompatible with (2, 2) input.
  3. Final Answer:

    The shape parameter is incompatible with the input array size. -> Option A
  4. Quick Check:

    Shape must match input array size [OK]
Quick Trick: Ensure shape matches input array dimensions [OK]
Common Mistakes:
MISTAKES
  • Assuming lists cannot be input to csr_matrix
  • Thinking csr_matrix disallows shape parameter
  • Misreading print statement as error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes