0
0
SciPydata~10 mins

CSC format (Compressed Sparse Column) 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 import the CSC matrix class from scipy.

SciPy
from scipy.sparse import [1]
Drag options to blanks, or click blank then click option'
Acsr_matrix
Bdok_matrix
Ccsc_matrix
Dcoo_matrix
Attempts:
3 left
💡 Hint
Common Mistakes
Importing csr_matrix instead of csc_matrix
Using coo_matrix which is a different sparse format
2fill in blank
medium

Complete the code to create a 3x3 CSC matrix with data [1, 2, 3] at positions (0,0), (1,1), and (2,2).

SciPy
from scipy.sparse import csc_matrix
import numpy as np
row = np.array([0, 1, 2])
col = np.array([0, 1, 2])
data = np.array([1, 2, 3])
matrix = csc_matrix(([1], (row, col)), shape=(3, 3))
Drag options to blanks, or click blank then click option'
Arow
Bdata
Ccol
D[1, 2, 3]
Attempts:
3 left
💡 Hint
Common Mistakes
Passing row or col arrays instead of data
Passing a list instead of the data variable
3fill in blank
hard

Fix the error in the code to convert a dense numpy array to a CSC matrix.

SciPy
import numpy as np
from scipy.sparse import csc_matrix
dense = np.array([[0, 1], [2, 0]])
sparse = [1](dense)
Drag options to blanks, or click blank then click option'
Acsr_matrix
Bdok_matrix
Ccoo_matrix
Dcsc_matrix
Attempts:
3 left
💡 Hint
Common Mistakes
Using csr_matrix instead of csc_matrix
Trying to call a method that does not exist
4fill in blank
hard

Fill both blanks to create a CSC matrix and then get its shape.

SciPy
from scipy.sparse import [1]
import numpy as np
data = np.array([4, 5, 6])
row = np.array([0, 1, 2])
col = np.array([0, 1, 2])
matrix = [2]((data, (row, col)), shape=(3, 3))
shape = matrix.shape
Drag options to blanks, or click blank then click option'
Acsc_matrix
Bcsr_matrix
Ccoo_matrix
Ddok_matrix
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing import and constructor classes
Using csr_matrix or other formats
5fill in blank
hard

Fill all three blanks to create a CSC matrix, convert it to dense, and print the result.

SciPy
from scipy.sparse import [1]
data = [7, 8, 9]
row = [0, 1, 2]
col = [0, 1, 2]
matrix = [2]((data, (row, col)), shape=(3, 3))
dense_matrix = matrix.[3]()
print(dense_matrix)
Drag options to blanks, or click blank then click option'
Acsc_matrix
Ctoarray
Dtodense
Attempts:
3 left
💡 Hint
Common Mistakes
Using toarray() instead of todense()
Importing wrong sparse matrix class