0
0
SciPydata~10 mins

Sparse matrix file I/O in SciPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aload_npz
Bload
Csave
Dsave_npz
Attempts:
3 left
💡 Hint
Common Mistakes
Using load_npz instead of save_npz.
Using generic save instead of save_npz.
2fill in blank
medium

Complete 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'
Aload_npz
Bsave_npz
Cload
Dsave
Attempts:
3 left
💡 Hint
Common Mistakes
Using save_npz instead of load_npz.
Using generic load instead of load_npz.
3fill in blank
hard

Fix 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'
Amatrix.todense()
Bmatrix.toarray()
Cmatrix
Dmatrix.data
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a dense array instead of a sparse matrix.
Passing the data attribute of the sparse matrix.
4fill in blank
hard

Fill 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'
Asave_npz
Bload_npz
Csave
Dload
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing save and load functions incorrectly.
Using generic save or load instead of npz-specific functions.
5fill in blank
hard

Fill 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'
Acsr_matrix
Bsave_npz
Cload_npz
Dcsc_matrix
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.