Challenge - 5 Problems
Scipy IO Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of loading a saved .mat file?
Consider the following Python code that saves a dictionary to a .mat file and then loads it back. What will be the output of the print statement?
SciPy
import numpy as np from scipy.io import savemat, loadmat my_dict = {'array': np.array([1, 2, 3])} savemat('test.mat', my_dict) loaded = loadmat('test.mat') print(loaded['array'])
Attempts:
2 left
💡 Hint
Remember how numpy arrays are stored and loaded in MATLAB format. The shape might change.
✗ Incorrect
When saving a 1D numpy array with savemat, it is stored as a 2D array with shape (n, 1). Loading it back returns a 2D array. Here, it is loaded as a column vector with shape (3,1), so printing shows [[1]
[2]
[3]].
❓ data_output
intermediateHow many keys are in the loaded .mat file dictionary?
After running the following code, how many keys does the loaded dictionary contain?
SciPy
from scipy.io import savemat, loadmat data = {'x': [1, 2], 'y': [3, 4]} savemat('data.mat', data) loaded_data = loadmat('data.mat') print(len(loaded_data.keys()))
Attempts:
2 left
💡 Hint
loadmat adds some default keys starting with '__'.
✗ Incorrect
loadmat returns a dictionary with the saved keys plus some metadata keys like '__header__', '__version__', and '__globals__'. So total keys are original 2 plus 3 metadata keys = 5 keys.
🔧 Debug
advancedWhy does this code raise a FileNotFoundError?
Examine the code below. Why does it raise a FileNotFoundError?
SciPy
from scipy.io import loadmat loaded = loadmat('nonexistent_file.mat')
Attempts:
2 left
💡 Hint
Check if the file path is correct and the file exists.
✗ Incorrect
FileNotFoundError occurs because the specified file does not exist in the directory where the code runs. loadmat expects the file to exist.
🚀 Application
advancedWhich option correctly saves multiple numpy arrays to a .mat file?
You have two numpy arrays, a and b. Which code snippet correctly saves both arrays into a single .mat file named 'arrays.mat'?
SciPy
import numpy as np from scipy.io import savemat a = np.array([1, 2, 3]) b = np.array([4, 5, 6])
Attempts:
2 left
💡 Hint
savemat expects a dictionary mapping variable names to arrays.
✗ Incorrect
savemat requires a dictionary where keys are variable names and values are numpy arrays. Option A correctly passes a dictionary with keys 'a' and 'b'. Option A passes a list, which is invalid. Option A uses keyword arguments which savemat does not accept. Option A nests arrays inside a list under one key, which saves only one variable.
🧠 Conceptual
expertWhat is the main limitation of using scipy.io.loadmat for loading MATLAB files?
Which of the following is a key limitation when using scipy.io.loadmat to load MATLAB .mat files?
Attempts:
2 left
💡 Hint
Check the file format versions supported by scipy.io.loadmat.
✗ Incorrect
scipy.io.loadmat cannot read MATLAB .mat files saved in version 7.3 or later because those files use the HDF5 format, which loadmat does not support. For those files, h5py or MATLAB engine is needed.
