0
0
SciPydata~10 mins

COO format (Coordinate) 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 create a COO sparse matrix from data, row, and col arrays.

SciPy
from scipy.sparse import [1]
data = [4, 5, 7]
row = [0, 1, 2]
col = [1, 2, 0]
sparse_matrix = [1]((data, (row, col)), shape=(3, 3))
Drag options to blanks, or click blank then click option'
Adok_matrix
Bcsr_matrix
Ccsc_matrix
Dcoo_matrix
Attempts:
3 left
💡 Hint
Common Mistakes
Using CSR or CSC format constructors instead of COO.
Not passing the data and (row, col) tuple correctly.
2fill in blank
medium

Complete the code to access the row indices of the COO sparse matrix.

SciPy
from scipy.sparse import coo_matrix
data = [1, 2, 3]
row = [0, 1, 2]
col = [2, 0, 1]
sparse = coo_matrix((data, (row, col)), shape=(3, 3))
rows = sparse.[1]
Drag options to blanks, or click blank then click option'
Arow
Bshape
Ccol
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using col instead of row.
Trying to access row indices via shape or data.
3fill in blank
hard

Fix the error in the code to correctly create a COO matrix from data and indices.

SciPy
from scipy.sparse import coo_matrix
data = [10, 20]
row = [0, 1]
col = [1, 0]
sparse = coo_matrix([1], shape=(2, 2))
Drag options to blanks, or click blank then click option'
A(data, (row, col))
Bdata, (row, col)
C[data, (row, col)]
D(data, row, col)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing data, row, col as separate arguments.
Using a list instead of a tuple for the first argument.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each nonzero element's (row, col) to its value.

SciPy
from scipy.sparse import coo_matrix
data = [3, 6, 9]
row = [0, 1, 2]
col = [2, 0, 1]
sparse = coo_matrix((data, (row, col)), shape=(3, 3))
coord_dict = { [1]: sparse.data[i] for i in range(len(sparse.data)) }
Drag options to blanks, or click blank then click option'
A(sparse.row[i], sparse.col[i])
B(sparse.row[i], sparse.data[i])
D(sparse.col[i], sparse.row[i])
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping row and col in the key tuple.
Using data instead of col in the key.
5fill in blank
hard

Fill all three blanks to create a COO matrix and convert it to a dense array.

SciPy
from scipy.sparse import [1]
data = [1, 2, 3]
row = [0, 1, 2]
col = [2, 0, 1]
sparse = [2]((data, (row, col)), shape=(3, 3))
dense = sparse.[3]()
Drag options to blanks, or click blank then click option'
Acoo_matrix
Ctoarray
Dtodense
Attempts:
3 left
💡 Hint
Common Mistakes
Using todense() which returns a matrix, not an array.
Forgetting to import coo_matrix.