The csr_matrix expects a 2D array or matrix-like input, but a 1D list is given.
Step 2: Understand error cause
Passing a 1D list causes shape ambiguity. The input should be reshaped or given as 2D, e.g., [[1,0,0,2,0,3]].
Final Answer:
The input should be a 2D array, not a 1D list -> Option A
Quick Check:
csr_matrix needs 2D input = C [OK]
Hint: csr_matrix input must be 2D array or matrix [OK]
Common Mistakes:
Passing 1D list directly
Ignoring input shape requirements
Assuming csr_matrix accepts any iterable
5. You have a large dataset with many zero values stored as a dense NumPy array. Which approach best converts it to a sparse matrix and then back to a dense array without losing data?
hard
A. Use csc_matrix to convert, then .todense() to convert back
B. Use csr_matrix to convert, then .toarray() to convert back
C. Use dok_matrix to convert, then .to_list() to convert back
D. Use lil_matrix to convert, then .to_numpy() to convert back
Solution
Step 1: Choose correct sparse format for conversion
csr_matrix is efficient for large sparse data and supports easy conversion back to dense.
Step 2: Convert back to dense array
Use .toarray() method on the sparse matrix to get the original dense NumPy array without data loss.
Final Answer:
Use csr_matrix to convert, then .toarray() to convert back -> Option B
Quick Check:
csr_matrix + toarray() preserves data = A [OK]
Hint: csr_matrix and toarray() convert sparse to dense safely [OK]
Common Mistakes:
Using .todense() which returns matrix, not array
Using .to_list() which does not exist
Assuming .to_numpy() works on sparse matrices
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.