0
0
Data Analysis Pythondata~10 mins

Sparse data handling in Data Analysis Python - Interactive Code 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.

Data Analysis Python
from scipy.sparse import [1]
Drag options to blanks, or click blank then click option'
Acsr_matrix
Barray
Cmatrix
Dsparse_matrix
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'array' which is a dense structure.
Using 'matrix' which is deprecated in SciPy.
Using 'sparse_matrix' which is not a valid class.
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).

Data Analysis Python
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))
Drag options to blanks, or click blank then click option'
Arows
Bcols
Cdata
D[1, 2, 3]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'rows' or 'cols' instead of the actual data values.
Using a list literal instead of the data variable.
3fill in blank
hard

Fix the error in the code to convert a dense NumPy array to a sparse CSR matrix.

Data Analysis Python
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 the shape instead of the array.
Passing a list instead of a NumPy array.
Passing a flattened array which loses 2D structure.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

Data Analysis Python
words = ['data', 'is', 'fun', 'and', 'useful']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword == 'data'
Dword in words
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as value instead of length.
Using a condition unrelated to length.
Using 'word in words' which is always true.
5fill in blank
hard

Fill all three blanks to create a filtered dictionary of word counts keeping only words with count greater than 1.

Data Analysis Python
word_counts = {'data': 3, 'is': 1, 'fun': 2, 'and': 1}
filtered = { [1]: [2] for [3], [2] in word_counts.items() if [2] > 1 }
Drag options to blanks, or click blank then click option'
Aword
Bcount
Cword_counts
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'item' instead of unpacking into word and count.
Using 'word_counts' as loop variable instead of items.
Not filtering counts greater than 1.