Challenge - 5 Problems
Sparse Matrix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of sparse matrix multiplication
What is the output of this code that multiplies two sparse matrices?
SciPy
import numpy as np from scipy.sparse import csr_matrix A = csr_matrix([[0, 0, 1], [1, 0, 0], [0, 2, 0]]) B = csr_matrix([[1, 0, 0], [0, 3, 0], [4, 0, 0]]) C = A.dot(B) print(C.toarray())
Attempts:
2 left
💡 Hint
Remember that sparse matrix multiplication follows the same rules as dense matrix multiplication.
✗ Incorrect
The multiplication of sparse matrices A and B results in matrix C where each element is the dot product of rows of A and columns of B. The output matches option D.
❓ data_output
intermediate1:30remaining
Number of non-zero elements after addition
Given two sparse matrices, what is the number of non-zero elements in their sum?
SciPy
from scipy.sparse import csc_matrix X = csc_matrix([[0, 5, 0], [0, 0, 0], [7, 0, 0]]) Y = csc_matrix([[0, 0, 3], [0, 0, 0], [0, 0, 0]]) Z = X + Y print(Z.nnz)
Attempts:
2 left
💡 Hint
Count unique non-zero positions after addition.
✗ Incorrect
Matrix X has 2 non-zero elements, Y has 1 non-zero element at a different position. Their sum has 3 unique non-zero elements, but since addition does not overlap, total nnz is 3. However, the code prints 3, so option B seems correct. But option B says 4, which is incorrect. Rechecking: X has 2 non-zero elements, Y has 1, total 3 unique positions. So correct answer is 3.
🔧 Debug
advanced1:30remaining
Identify the error in sparse matrix slicing
What error does this code raise when slicing a sparse matrix?
SciPy
from scipy.sparse import lil_matrix M = lil_matrix((3,3)) M[0, 0] = 1 M[1, 1] = 2 sub = M[0:2, 0:2] print(sub)
Attempts:
2 left
💡 Hint
Check if lil_matrix supports slicing.
✗ Incorrect
The lil_matrix supports slicing and returns a sparse matrix of the sliced part. So no error occurs and the code prints the sliced sparse matrix.
❓ visualization
advanced1:30remaining
Visualizing sparsity pattern
Which code snippet correctly visualizes the sparsity pattern of a CSR sparse matrix using matplotlib?
SciPy
import matplotlib.pyplot as plt from scipy.sparse import csr_matrix A = csr_matrix([[0, 0, 1], [1, 0, 0], [0, 2, 0]]) # Visualization code here
Attempts:
2 left
💡 Hint
Use the function designed to show sparsity patterns.
✗ Incorrect
plt.spy() is the correct function to visualize the sparsity pattern of a sparse matrix. Other options either cause errors or do not show sparsity correctly.
🚀 Application
expert3:00remaining
Efficient sparse matrix row normalization
Which code snippet correctly normalizes each row of a CSR sparse matrix so that each row sums to 1, without converting to dense format?
SciPy
from scipy.sparse import csr_matrix import numpy as np M = csr_matrix([[0, 1, 2], [3, 0, 0], [0, 0, 4]]) # Normalize rows here
Attempts:
2 left
💡 Hint
Use the sparse matrix data and indices to normalize efficiently.
✗ Incorrect
Option A correctly computes row sums and divides non-zero elements by their row sum using indexing. Option A and D cause errors or incorrect broadcasting. Option A tries to assign to sparse matrix slices which is not supported.