0
0
SciPydata~10 mins

Converting between formats 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 convert a dense matrix to a CSR sparse matrix.

SciPy
from scipy.sparse import csr_matrix
import numpy as np

dense_matrix = np.array([[1, 0, 0], [0, 2, 0], [0, 0, 3]])
sparse_matrix = csr_matrix([1])
print(sparse_matrix)
Drag options to blanks, or click blank then click option'
Adense_matrix
Bcsr_matrix
Cnp.array
Dsparse_matrix
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the function name instead of the variable.
Passing the sparse matrix variable to itself.
Using np.array inside csr_matrix instead of the variable.
2fill in blank
medium

Complete the code to convert a CSR sparse matrix back to a dense numpy array.

SciPy
from scipy.sparse import csr_matrix
import numpy as np

sparse_matrix = csr_matrix([[0, 0, 1], [1, 0, 0], [0, 2, 0]])
dense_matrix = sparse_matrix.[1]()
print(dense_matrix)
Drag options to blanks, or click blank then click option'
Ato_numpy
Btodense
Ctoarray
Dtodense_array
Attempts:
3 left
💡 Hint
Common Mistakes
Using todense() which returns a matrix, not a numpy array.
Using a non-existent method like to_numpy().
Forgetting the parentheses after the method.
3fill in blank
hard

Fix the error in the code to convert a COO sparse matrix to CSC format.

SciPy
from scipy.sparse import coo_matrix

row = [0, 3, 1, 0]
col = [0, 3, 1, 2]
data = [4, 5, 7, 9]
coo = coo_matrix((data, (row, col)), shape=(4, 4))
csc = coo.[1]()
print(csc)
Drag options to blanks, or click blank then click option'
Atocsc
Btocsr
Ctoarray
Dtodense
Attempts:
3 left
💡 Hint
Common Mistakes
Using tocsr() which converts to CSR format instead.
Using toarray() or todense() which convert to dense formats.
Forgetting parentheses after the method.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.

SciPy
words = ['data', 'is', 'fun', 'and', 'powerful']
lengths = {word: [1] for word in words if [2]
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Clen(word) > 3
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Checking if the word is greater than 3 instead of its length.
Omitting the condition or using incorrect syntax.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each word in uppercase to its length only if the length is greater than 2.

SciPy
words = ['cat', 'a', 'dog', 'elephant']
result = { [1]: [2] for word in words if [3] }
print(result)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Clen(word) > 2
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the key instead of uppercase.
Using the word instead of length as the value.
Incorrect condition syntax or missing condition.