Bird
Raised Fist0
SciPydata~10 mins

COO format (Coordinate) in SciPy - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. What does the COO format in scipy primarily store?
coo_matrix is used to represent sparse matrices by storing which of the following?
easy
A. Only non-zero values and their row and column indices
B. All values including zeros in a dense matrix
C. Only the diagonal elements of a matrix
D. The matrix in a compressed row format

Solution

  1. Step 1: Understand COO format basics and compare options

    COO format stores sparse matrices by keeping track of non-zero values and their positions. Only non-zero values and their row and column indices correctly describes storing non-zero values with their row and column indices.
  2. Final Answer:

    Only non-zero values and their row and column indices -> Option A
  3. Quick Check:

    COO = non-zero values + coordinates [OK]
Hint: COO stores non-zero values with positions only [OK]
Common Mistakes:
  • Thinking COO stores all matrix values
  • Confusing COO with compressed formats
  • Assuming COO stores only diagonal elements
2. Which of the following is the correct way to create a COO sparse matrix from data arrays data, row, and col using scipy?
easy
A. coo_matrix((data, (row, col)))
B. coo_matrix(data, row, col)
C. coo_matrix(data, (row, col))
D. coo_matrix((row, col), data)

Solution

  1. Step 1: Recall scipy coo_matrix syntax and check each option

    The correct syntax is coo_matrix((data, (row, col))) where data is values and (row, col) are coordinates. Only coo_matrix((data, (row, col))) matches the correct tuple structure required by coo_matrix.
  2. Final Answer:

    coo_matrix((data, (row, col))) -> Option A
  3. Quick Check:

    Tuple of (data, (row, col)) is correct [OK]
Hint: Use tuple: (data, (row, col)) for coo_matrix [OK]
Common Mistakes:
  • Passing data, row, col as separate arguments
  • Swapping order of data and coordinates
  • Not using nested tuple for coordinates
3. What is the output of the following code?
from scipy.sparse import coo_matrix
row = [0, 1, 2]
col = [1, 2, 0]
data = [4, 5, 6]
coo = coo_matrix((data, (row, col)), shape=(3, 3))
print(coo.toarray())
medium
A. [[0 6 0] [5 0 0] [0 0 4]]
B. [[4 0 0] [0 5 0] [0 0 6]]
C. [[0 0 6] [4 0 0] [0 5 0]]
D. [[0 4 0] [0 0 5] [6 0 0]]

Solution

  1. Step 1: Map data to matrix positions and construct full matrix

    Data 4 at (0,1), 5 at (1,2), 6 at (2,0) means matrix has these values at those coordinates. Positions without data are zero, so the matrix is [[0 4 0], [0 0 5], [6 0 0]].
  2. Final Answer:

    [[0 4 0] [0 0 5] [6 0 0]] -> Option D
  3. Quick Check:

    Data placed at (row, col) = matrix [OK]
Hint: Match data with (row, col) indices to build matrix [OK]
Common Mistakes:
  • Mixing row and column indices
  • Assuming data fills diagonal
  • Confusing toarray() output formatting
4. Identify the error in this code snippet that tries to create a COO matrix:
from scipy.sparse import coo_matrix
row = [0, 1]
col = [1, 2]
data = [3]
coo = coo_matrix((data, (row, col)), shape=(3, 3))
medium
A. Shape parameter is missing
B. Length of data does not match length of row and col arrays
C. coo_matrix requires dense matrix input
D. row and col must be numpy arrays, not lists

Solution

  1. Step 1: Check lengths of data, row, and col and understand coo_matrix requirements

    Data has length 1, but row and col have length 2, which is inconsistent. Data, row, and col must have the same length to map values correctly.
  2. Final Answer:

    Length of data does not match length of row and col arrays -> Option B
  3. Quick Check:

    Data, row, col lengths must match [OK]
Hint: Data, row, col arrays must be same length [OK]
Common Mistakes:
  • Ignoring length mismatch errors
  • Assuming lists are invalid input
  • Missing shape parameter causes error
5. You have coordinate data for a large sparse matrix with many zero values. Which of the following is the best approach to efficiently create and store this matrix using scipy?
hard
A. Create a dense numpy array and convert it to coo_matrix
B. Store the matrix as a list of lists with zeros included
C. Use coo_matrix with data, row, and col arrays and convert to CSR format for fast arithmetic
D. Use a dictionary to store all matrix values including zeros

Solution

  1. Step 1: Understand sparse matrix creation, optimize for operations, and evaluate options

    COO format efficiently stores non-zero values with coordinates, ideal for sparse data. Converting COO to CSR format improves arithmetic and slicing performance. Dense arrays waste memory; lists or dicts storing zeros are inefficient.
  2. Final Answer:

    Use coo_matrix with data, row, and col arrays and convert to CSR format for fast arithmetic -> Option C
  3. Quick Check:

    COO + CSR = efficient sparse matrix handling [OK]
Hint: Create COO then convert to CSR for speed [OK]
Common Mistakes:
  • Using dense arrays wastes memory
  • Storing zeros wastes space
  • Ignoring format conversion for speed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard