0
0
SciPydata~10 mins

Creating sparse matrices in SciPy - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the sparse matrix module from scipy.

SciPy
from scipy import [1]
Drag options to blanks, or click blank then click option'
Astats
Barray
Csparse
Doptimize
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unrelated modules like scipy.array or scipy.stats.
Forgetting to import the sparse module before using it.
2fill in blank
medium

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

SciPy
from scipy.sparse import coo_matrix

row = [0, 1, 2]
col = [0, 1, 2]
data = [1, 2, 3]
sparse_matrix = coo_matrix(([1], (row, col)), shape=(3, 3))
Drag options to blanks, or click blank then click option'
Adata
Brow
Ccol
D[1, 2, 3]
Attempts:
3 left
💡 Hint
Common Mistakes
Passing row or col instead of data as the first argument.
Passing a list literal instead of the variable holding data.
3fill in blank
hard

Fix the error in the code to create a CSR sparse matrix from dense data.

SciPy
import numpy as np
from scipy.sparse import csr_matrix

dense = np.array([[0, 0, 1], [1, 0, 0], [0, 2, 0]])
sparse = csr_matrix([1])
Drag options to blanks, or click blank then click option'
Adense.tolist()
Bdense.shape
Cdense.flatten()
Ddense
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a list instead of a numpy array causes errors.
Passing the shape instead of the data.
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', 'science', 'is', 'fun']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Clen(word) < 3
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using the wrong condition like less than 3.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their counts only if the count is greater than 1.

SciPy
word_counts = {'data': 2, 'science': 1, 'fun': 3}
result = [1]: [2] for [3], [2] in word_counts.items() if [2] > 1}
Drag options to blanks, or click blank then click option'
Aword.upper()
Bcount
Cword
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong loop variable like 'item' instead of 'word'.
Not converting words to uppercase.
Using the wrong variable in the condition.