Complete the code to import the sparse matrix module from SciPy.
from scipy.sparse import [1]
The csr_matrix is a common sparse matrix format in SciPy used for efficient storage.
Complete the code to create a 3x3 sparse matrix with data [1, 2, 3] at positions (0,0), (1,1), and (2,2).
from scipy.sparse import csr_matrix import numpy as np rows = np.array([0, 1, 2]) cols = np.array([0, 1, 2]) data = np.array([1, 2, 3]) sparse_mat = csr_matrix(([1], (rows, cols)), shape=(3, 3))
The first argument to csr_matrix is the data array containing the non-zero values.
Fix the error in the code to convert a dense NumPy array to a sparse CSR matrix.
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])
The csr_matrix constructor accepts a dense NumPy array directly to convert it to sparse format.
Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.
words = ['data', 'is', 'fun', 'and', 'useful'] lengths = {word: [1] for word in words if [2]
The comprehension maps each word to its length only if the length is greater than 3.
Fill all three blanks to create a filtered dictionary of word counts keeping only words with count greater than 1.
word_counts = {'data': 3, 'is': 1, 'fun': 2, 'and': 1}
filtered = { [1]: [2] for [3], [2] in word_counts.items() if [2] > 1 }The comprehension iterates over word_counts.items() unpacking into word and count. It keeps only words with count > 1.