Challenge - 5 Problems
Sparse Matrix Conversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of converting dense to CSR format
What is the output of this code that converts a dense matrix to CSR format and prints its data array?
SciPy
import numpy as np from scipy.sparse import csr_matrix dense = np.array([[0, 0, 1], [2, 0, 0], [0, 3, 0]]) sparse_csr = csr_matrix(dense) print(sparse_csr.data)
Attempts:
2 left
💡 Hint
CSR format stores only non-zero values in the data array.
✗ Incorrect
The csr_matrix.data attribute contains only the non-zero elements of the matrix in row-major order. Here, the non-zero elements are 1, 2, and 3.
❓ data_output
intermediate2:00remaining
Shape after converting COO to CSC format
Given a sparse matrix in COO format with shape (4, 5), what is the shape of the matrix after converting it to CSC format?
SciPy
from scipy.sparse import coo_matrix row = [0, 3, 1, 0] col = [0, 3, 1, 2] data = [4, 5, 7, 9] coo = coo_matrix((data, (row, col)), shape=(4, 5)) csc = coo.tocsc() print(csc.shape)
Attempts:
2 left
💡 Hint
Converting between sparse formats does not change the matrix shape.
✗ Incorrect
The shape of the matrix remains the same when converting between sparse formats like COO to CSC.
🔧 Debug
advanced2:00remaining
Identify the error when converting dense to LIL format
What error does this code raise when converting a list of lists to a LIL sparse matrix?
SciPy
from scipy.sparse import lil_matrix dense_list = [[1, 0], [0, 2]] sparse_lil = lil_matrix(dense_list) print(sparse_lil)
Attempts:
2 left
💡 Hint
Check if scipy.sparse.lil_matrix accepts list of lists as input.
✗ Incorrect
scipy.sparse.lil_matrix can accept a list of lists as input and convert it to a sparse matrix without error.
🚀 Application
advanced2:00remaining
Best sparse format for fast row slicing
Which sparse matrix format is best suited for fast row slicing operations on large sparse matrices?
Attempts:
2 left
💡 Hint
Think about how data is stored internally for efficient row access.
✗ Incorrect
CSR format stores data by rows, making row slicing very efficient compared to other formats.
🧠 Conceptual
expert2:00remaining
Memory difference between COO and CSR formats
Which statement correctly describes the memory usage difference between COO and CSR sparse matrix formats for the same data?
Attempts:
2 left
💡 Hint
Consider how indices are stored in COO vs CSR formats.
✗ Incorrect
COO format stores row and column indices for each non-zero element, which can use more memory than CSR that compresses row indices into a pointer array.