Challenge - 5 Problems
COO Format Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of COO matrix data arrays
Given the following code creating a COO sparse matrix, what will be the output of the data, row, and col arrays?
SciPy
from scipy.sparse import coo_matrix import numpy as np row = np.array([0, 3, 1, 0]) col = np.array([0, 3, 1, 2]) data = np.array([4, 5, 7, 9]) coo = coo_matrix((data, (row, col)), shape=(4, 4)) print(coo.data) print(coo.row) print(coo.col)
Attempts:
2 left
💡 Hint
The COO format stores data in the order you provide the row and column indices.
✗ Incorrect
The coo_matrix stores the data, row, and col arrays exactly as given during creation. So the output matches the input arrays.
❓ data_output
intermediate1:30remaining
Number of non-zero elements in COO matrix
How many non-zero elements does the following COO matrix have?
SciPy
from scipy.sparse import coo_matrix row = [0, 1, 1, 2, 3] col = [0, 0, 2, 3, 3] data = [1, 2, 3, 4, 5] coo = coo_matrix((data, (row, col)), shape=(4, 4)) print(coo.nnz)
Attempts:
2 left
💡 Hint
nnz means number of stored non-zero elements.
✗ Incorrect
The nnz attribute counts the number of non-zero entries stored, which is the length of the data array.
🔧 Debug
advanced2:00remaining
Identify the error in COO matrix creation
What error will this code raise when creating a COO matrix?
SciPy
from scipy.sparse import coo_matrix row = [0, 1, 2] col = [0, 1] data = [1, 2, 3] coo = coo_matrix((data, (row, col)), shape=(3, 3))
Attempts:
2 left
💡 Hint
Check if the lengths of row, col, and data arrays match.
✗ Incorrect
The row and data arrays have length 3, but col has length 2, causing a shape mismatch error.
🚀 Application
advanced1:30remaining
Convert COO matrix to dense and find sum
What is the sum of all elements in the dense matrix converted from this COO matrix?
SciPy
from scipy.sparse import coo_matrix row = [0, 1, 2, 2] col = [0, 1, 1, 2] data = [10, 20, 30, 40] coo = coo_matrix((data, (row, col)), shape=(3, 3)) dense = coo.toarray() print(dense.sum())
Attempts:
2 left
💡 Hint
Sum all the data values since zeros do not add.
✗ Incorrect
Sum of data array is 10+20+30+40=100, which is the sum of all elements in the dense matrix.
🧠 Conceptual
expert2:30remaining
Effect of duplicate entries in COO matrix
If a COO matrix has duplicate entries at the same row and column indices, what happens when converting it to CSR format?
Attempts:
2 left
💡 Hint
Think about how sparse formats handle duplicates when compressed.
✗ Incorrect
When converting COO to CSR, duplicate entries at the same position are summed into a single value.