Challenge - 5 Problems
Sparse Matrix File I/O Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of saving and loading a sparse matrix
What will be the output shape of the sparse matrix after saving and loading it using scipy's
save_npz and load_npz?SciPy
import numpy as np from scipy.sparse import csr_matrix, save_npz, load_npz matrix = csr_matrix(np.array([[0, 0, 1], [1, 0, 0], [0, 2, 0]])) save_npz('test_matrix.npz', matrix) loaded_matrix = load_npz('test_matrix.npz') print(loaded_matrix.shape)
Attempts:
2 left
💡 Hint
The shape of the matrix does not change when saving and loading with scipy sparse functions.
✗ Incorrect
The
save_npz and load_npz functions preserve the sparse matrix structure and shape exactly. The original matrix is 3 rows by 3 columns, so the loaded matrix shape is (3, 3).❓ data_output
intermediate2:00remaining
Data content after loading sparse matrix
After saving and loading the sparse matrix below, what will be the value at position (1, 0) in the loaded matrix?
SciPy
import numpy as np from scipy.sparse import csr_matrix, save_npz, load_npz matrix = csr_matrix(np.array([[0, 0, 1], [1, 0, 0], [0, 2, 0]])) save_npz('test_matrix.npz', matrix) loaded_matrix = load_npz('test_matrix.npz') print(loaded_matrix[1, 0])
Attempts:
2 left
💡 Hint
Check the original matrix value at row 1, column 0.
✗ Incorrect
The original matrix has value 1 at position (1, 0). Saving and loading does not change values, so the loaded matrix at (1, 0) is 1.
🔧 Debug
advanced2:00remaining
Identify the error when loading a sparse matrix
What error will occur when trying to load a sparse matrix from a file that does not exist using
load_npz?SciPy
from scipy.sparse import load_npz loaded_matrix = load_npz('nonexistent_file.npz')
Attempts:
2 left
💡 Hint
Think about what happens when you try to open a file that does not exist.
✗ Incorrect
Trying to load a file that does not exist raises a FileNotFoundError because the file system cannot find the specified file.
🚀 Application
advanced2:00remaining
Choosing the best file format for sparse matrix storage
You have a large sparse matrix that you want to save and share with others. Which file format is best to use for saving and loading sparse matrices efficiently with scipy?
Attempts:
2 left
💡 Hint
Consider which format preserves sparsity and is optimized for scipy sparse matrices.
✗ Incorrect
NPZ format with save_npz and load_npz is designed for sparse matrices. It preserves sparsity and is efficient in storage and speed. CSV, TXT, and JSON do not preserve sparsity and are inefficient for large sparse data.
🧠 Conceptual
expert3:00remaining
Understanding sparse matrix file I/O internals
Which of the following best describes what
save_npz stores when saving a scipy CSR sparse matrix?Attempts:
2 left
💡 Hint
Think about how CSR format represents sparse matrices internally.
✗ Incorrect
The CSR format stores three arrays: data (nonzero values), indices (column indices), and indptr (index pointers for row starts), plus the shape. save_npz saves these arrays and shape in a compressed NPZ file for efficient storage.