Challenge - 5 Problems
CSR Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of CSR matrix multiplication
What is the output of the following code that multiplies two CSR 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).toarray() print(C)
Attempts:
2 left
💡 Hint
Remember that CSR matrix multiplication follows normal matrix multiplication rules.
✗ Incorrect
Matrix A has non-zero elements at positions (0,2), (1,0), and (2,1). Matrix B has non-zero elements at (0,0), (1,1), and (2,0). Multiplying A and B, the element at (0,0) is A(0,2)*B(2,0) = 1*4=4, at (1,0) is A(1,0)*B(0,0)=1*1=1, and at (2,1) is A(2,1)*B(1,1)=2*3=6. All other elements are zero.
❓ data_output
intermediate1:00remaining
Number of non-zero elements in CSR matrix
Given the CSR matrix below, how many non-zero elements does it contain?
SciPy
from scipy.sparse import csr_matrix matrix = csr_matrix([[0, 5, 0], [0, 0, 0], [7, 0, 8]]) print(matrix.nnz)
Attempts:
2 left
💡 Hint
Count all the non-zero values in the matrix.
✗ Incorrect
The matrix has three non-zero values: 5 at (0,1), 7 at (2,0), and 8 at (2,2). So nnz (number of non-zero elements) is 3.
🔧 Debug
advanced1:30remaining
Identify the error in CSR matrix creation
What error will this code raise when trying to create a CSR matrix?
SciPy
from scipy.sparse import csr_matrix # Data and indices lengths mismatch data = [1, 2, 3] indices = [0, 2] indptr = [0, 2, 3] csr = csr_matrix((data, indices, indptr), shape=(2, 3))
Attempts:
2 left
💡 Hint
Check if the lengths of data and indices match.
✗ Incorrect
The CSR format requires that the data and indices arrays have the same length because each data value corresponds to an index. Here, data has length 3 but indices has length 2, causing a ValueError.
🧠 Conceptual
advanced1:30remaining
Understanding CSR matrix components
Which of the following correctly describes the role of the 'indptr' array in a CSR matrix?
Attempts:
2 left
💡 Hint
Think about how CSR stores rows efficiently.
✗ Incorrect
'indptr' is an array of length (number_of_rows + 1). Each element points to the index in 'data' and 'indices' arrays where a row starts and ends. This allows quick access to each row's non-zero elements.
🚀 Application
expert2:00remaining
Efficient row slicing in CSR matrix
You want to extract the second row from a large CSR matrix efficiently. Which code snippet correctly extracts the second row as a CSR matrix?
SciPy
from scipy.sparse import csr_matrix matrix = csr_matrix([[0, 1, 0], [2, 0, 3], [0, 0, 4]])
Attempts:
2 left
💡 Hint
Look for a method designed for efficient row extraction in CSR.
✗ Incorrect
matrix.getrow(1) returns the second row as a CSR matrix efficiently without converting the whole matrix to dense. matrix[1, :] returns a 1xN sparse matrix but can be less efficient. matrix[1] returns a 1D sparse matrix (not CSR). matrix.toarray()[1] converts entire matrix to dense, which is inefficient for large sparse matrices.