Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to save a sparse matrix to a file using scipy.
SciPy
from scipy import sparse matrix = sparse.csr_matrix([[0, 0, 1], [1, 0, 0]]) sparse.[1]('matrix.npz', matrix)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using load_npz instead of save_npz.
Using generic save instead of save_npz.
✗ Incorrect
The save_npz function saves a sparse matrix to a file in .npz format.
2fill in blank
mediumComplete the code to load a sparse matrix from a file using scipy.
SciPy
from scipy import sparse loaded_matrix = sparse.[1]('matrix.npz')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using save_npz instead of load_npz.
Using generic load instead of load_npz.
✗ Incorrect
The load_npz function loads a sparse matrix from a .npz file.
3fill in blank
hardFix the error in the code to correctly save a sparse matrix to a file.
SciPy
from scipy import sparse matrix = sparse.csr_matrix([[1, 0], [0, 1]]) sparse.save_npz('data.npz', [1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a dense array instead of a sparse matrix.
Passing the data attribute of the sparse matrix.
✗ Incorrect
The save_npz function requires a sparse matrix, not a dense array or data attribute.
4fill in blank
hardFill both blanks to save and then load a sparse matrix correctly.
SciPy
from scipy import sparse matrix = sparse.csr_matrix([[0, 2], [3, 0]]) sparse.[1]('file.npz', matrix) loaded = sparse.[2]('file.npz')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing save and load functions incorrectly.
Using generic save or load instead of npz-specific functions.
✗ Incorrect
Use save_npz to save and load_npz to load sparse matrices.
5fill in blank
hardFill all three blanks to create, save, and load a sparse matrix, then convert it to dense.
SciPy
from scipy import sparse matrix = sparse.[1]([[1, 0], [0, 1]]) sparse.[2]('matrix.npz', matrix) loaded = sparse.[3]('matrix.npz') dense = loaded.toarray()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using csc_matrix instead of csr_matrix for creation.
Using generic save/load instead of save_npz/load_npz.
✗ Incorrect
Create the sparse matrix with csr_matrix, save with save_npz, and load with load_npz.