Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to save a NumPy array to a .mat file using scipy.io.
SciPy
import numpy as np from scipy import io arr = np.array([1, 2, 3]) io.[1]('data.mat', {'arr': arr})
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using loadmat instead of savemat to save data.
Using save or load which are not scipy.io functions.
✗ Incorrect
The savemat function saves data to a MATLAB .mat file.
2fill in blank
mediumComplete the code to load data from a .mat file using scipy.io.
SciPy
from scipy import io data = io.[1]('data.mat') print(data['arr'])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using savemat instead of loadmat to load data.
Using save or load which are not scipy.io functions.
✗ Incorrect
The loadmat function loads data from a MATLAB .mat file.
3fill in blank
hardFix the error in the code to correctly save two arrays to a .mat file.
SciPy
import numpy as np from scipy import io x = np.array([1, 2]) y = np.array([3, 4]) io.savemat('data.mat', [1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a list or tuple instead of a dictionary.
Passing variables without keys.
✗ Incorrect
The data must be passed as a dictionary mapping variable names to arrays.
4fill in blank
hardFill both blanks to save a dictionary with arrays and then load it back.
SciPy
from scipy import io import numpy as np data = {'a': np.array([1, 2]), 'b': np.array([3, 4])} io.[1]('file.mat', data) loaded = io.[2]('file.mat') print(loaded['a'])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up savemat and loadmat.
Using save or load which are not scipy.io functions.
✗ Incorrect
Use savemat to save and loadmat to load data.
5fill in blank
hardFill all three blanks to save an array with a custom name, load it, and print the loaded array.
SciPy
import numpy as np from scipy import io arr = np.array([10, 20, 30]) io.[1]('mydata.mat', [2]) loaded = io.[3]('mydata.mat') print(loaded['my_array'])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using a dictionary with the correct key name.
Using wrong function names for saving or loading.
✗ Incorrect
Use savemat to save with a dictionary naming the array, then loadmat to load it back.