import numpy as np
from scipy.sparse import coo_matrix
row = np.array([0, 1, 2])
col = np.array([1, 2, 0])
data = np.array([4, 5, 6])
sparse_mat = coo_matrix((data, (row, col)), shape=(3, 3))
print(sparse_mat.toarray())
medium
A. [[0 4 0]
[0 0 5]
[6 0 0]]
B. [[4 0 0]
[0 5 0]
[0 0 6]]
C. [[0 0 6]
[4 0 0]
[0 5 0]]
D. [[0 6 0]
[5 0 0]
[0 0 4]]
Solution
Step 1: Map data to positions
Data 4 at (0,1), 5 at (1,2), 6 at (2,0) in a 3x3 matrix.
Step 2: Construct full matrix
Positions without data are zero, so matrix is [[0 4 0], [0 0 5], [6 0 0]].
Final Answer:
[[0 4 0]
[0 0 5]
[6 0 0]] -> Option A
Quick Check:
Data at (row,col) = matrix entries [OK]
Hint: Match data with (row, col) indices to build matrix [OK]
Common Mistakes:
Mixing row and column indices
Assuming data fills diagonal
Confusing CSR/CSC formats with COO
4. Identify the error in this code snippet:
from scipy.sparse import coo_matrix
row = [0, 1]
col = [1, 2]
data = [3, 4]
sparse = coo_matrix(data, (row, col))
medium
A. coo_matrix does not accept data with length 2
B. row and col should be numpy arrays, not lists
C. The shape parameter is missing
D. The data and indices should be passed as a single tuple
Solution
Step 1: Check coo_matrix constructor usage
coo_matrix expects a single tuple (data, (row, col)) as first argument.
Step 2: Identify mistake in code
Code passes data and (row, col) as separate arguments, causing error.
Final Answer:
The data and indices should be passed as a single tuple -> Option D
Quick Check:
coo_matrix((data, (row, col))) required [OK]
Hint: Pass data and indices as one tuple to coo_matrix [OK]
Common Mistakes:
Passing data and indices as separate arguments
Forgetting to wrap indices in a tuple
Assuming lists cause error instead of argument structure
5. You have a large dataset with many zero values. You want to create a sparse matrix for fast arithmetic operations. Which format should you choose and why?
Options: A) CSR format, because it is efficient for arithmetic and slicing B) COO format, because it is efficient for arithmetic C) DOK format, because it uses dictionary storage D) LIL format, because it is best for matrix multiplication
hard
A. DOK format, because it uses dictionary storage
B. COO format, because it is efficient for arithmetic
C. CSR format, because it is efficient for arithmetic and slicing
D. LIL format, because it is best for matrix multiplication
Solution
Step 1: Understand sparse matrix formats
COO is good for building matrices, CSR is efficient for arithmetic and slicing.
Step 2: Choose format for fast arithmetic
CSR format supports fast arithmetic operations and efficient row slicing, ideal for large data.
Final Answer:
CSR format, because it is efficient for arithmetic and slicing -> Option C
Quick Check:
CSR = fast arithmetic and slicing [OK]
Hint: Use CSR for fast math and slicing on sparse data [OK]
Common Mistakes:
Choosing COO for arithmetic instead of CSR
Confusing DOK with arithmetic efficiency
Assuming LIL is best for multiplication
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.