0
0
SciPydata~20 mins

Converting between formats in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sparse Matrix Conversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[0 0 1 2 0 0 0 3 0]
B[1 2 3]
C[1 2 3 0]
D[1 2]
Attempts:
2 left
💡 Hint
CSR format stores only non-zero values in the data array.
data_output
intermediate
2: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)
A(4, 4)
B(5, 4)
C(4, 5)
D(5, 5)
Attempts:
2 left
💡 Hint
Converting between sparse formats does not change the matrix shape.
🔧 Debug
advanced
2: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)
ATypeError: expected sequence of sequences, got list of lists
BValueError: object too deep for desired array
CAttributeError: 'list' object has no attribute 'tocsc'
DNo error, prints the LIL matrix correctly
Attempts:
2 left
💡 Hint
Check if scipy.sparse.lil_matrix accepts list of lists as input.
🚀 Application
advanced
2:00remaining
Best sparse format for fast row slicing
Which sparse matrix format is best suited for fast row slicing operations on large sparse matrices?
ACSR (Compressed Sparse Row)
BCSC (Compressed Sparse Column)
CCOO (Coordinate)
DDOK (Dictionary of Keys)
Attempts:
2 left
💡 Hint
Think about how data is stored internally for efficient row access.
🧠 Conceptual
expert
2: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?
ACOO uses more memory because it stores row and column indices separately for each non-zero element.
BCSR uses more memory because it stores an additional index pointer array for rows.
CBoth COO and CSR use the same amount of memory for the same data.
DCOO uses less memory because it stores data in a compressed form unlike CSR.
Attempts:
2 left
💡 Hint
Consider how indices are stored in COO vs CSR formats.