Complete the code to import the sparse matrix module from scipy.
from scipy import [1]
The sparse module in scipy is used to create and work with sparse matrices efficiently.
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.
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))
The first argument to coo_matrix is the data array containing the values to place in the sparse matrix.
Fix the error in the code to create a CSR sparse matrix from dense data.
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 into a sparse matrix.
Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.
words = ['data', 'science', 'is', 'fun'] lengths = {word: [1] for word in words if [2]
The dictionary comprehension maps each word to its length using len(word). The condition filters words with length greater than 3 using len(word) > 3.
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their counts only if the count is greater than 1.
word_counts = {'data': 2, 'science': 1, 'fun': 3}
result = [1]: [2] for [3], [2] in word_counts.items() if [2] > 1}The comprehension maps the uppercase version of each word (word.upper()) to its count (count). The loop variable is word and count from word_counts.items(). The condition filters counts greater than 1.