Complete the code to create a COO sparse matrix from data, row, and col arrays.
from scipy.sparse import [1] data = [4, 5, 7] row = [0, 1, 2] col = [1, 2, 0] sparse_matrix = [1]((data, (row, col)), shape=(3, 3))
The coo_matrix function creates a sparse matrix in COO format using data, row, and column arrays.
Complete the code to access the row indices of the COO sparse matrix.
from scipy.sparse import coo_matrix data = [1, 2, 3] row = [0, 1, 2] col = [2, 0, 1] sparse = coo_matrix((data, (row, col)), shape=(3, 3)) rows = sparse.[1]
col instead of row.shape or data.The row attribute of a COO matrix gives the array of row indices for nonzero elements.
Fix the error in the code to correctly create a COO matrix from data and indices.
from scipy.sparse import coo_matrix data = [10, 20] row = [0, 1] col = [1, 0] sparse = coo_matrix([1], shape=(2, 2))
The COO matrix constructor requires a tuple of (data, (row, col)) as the first argument.
Fill both blanks to create a dictionary comprehension that maps each nonzero element's (row, col) to its value.
from scipy.sparse import coo_matrix data = [3, 6, 9] row = [0, 1, 2] col = [2, 0, 1] sparse = coo_matrix((data, (row, col)), shape=(3, 3)) coord_dict = { [1]: sparse.data[i] for i in range(len(sparse.data)) }
The dictionary comprehension uses the tuple (row, col) as keys and the data values as values.
Fill all three blanks to create a COO matrix and convert it to a dense array.
from scipy.sparse import [1] data = [1, 2, 3] row = [0, 1, 2] col = [2, 0, 1] sparse = [2]((data, (row, col)), shape=(3, 3)) dense = sparse.[3]()
todense() which returns a matrix, not an array.coo_matrix.Use coo_matrix to create the sparse matrix and toarray() to convert it to a dense NumPy array.