0
0
SciPydata~20 mins

Saving and loading data (scipy.io) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Scipy IO Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What 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'])
A[1 2 3]
B
[[1]
 [2]
 [3]]
C[[1 2 3]]
DKeyError
Attempts:
2 left
💡 Hint
Remember how numpy arrays are stored and loaded in MATLAB format. The shape might change.
data_output
intermediate
1:30remaining
How 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()))
A2
B3
C5
D4
Attempts:
2 left
💡 Hint
loadmat adds some default keys starting with '__'.
🔧 Debug
advanced
1:00remaining
Why 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')
AThe loadmat function is deprecated and cannot open files.
Bloadmat requires the file to be in .npy format, not .mat.
CThe file path must be absolute, relative paths cause errors.
DThe file 'nonexistent_file.mat' does not exist in the current directory.
Attempts:
2 left
💡 Hint
Check if the file path is correct and the file exists.
🚀 Application
advanced
2:00remaining
Which 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])
Asavemat('arrays.mat', {'a': a, 'b': b})
Bsavemat('arrays.mat', [a, b])
Csavemat('arrays.mat', a=a, b=b)
Dsavemat('arrays.mat', {'arrays': [a, b]})
Attempts:
2 left
💡 Hint
savemat expects a dictionary mapping variable names to arrays.
🧠 Conceptual
expert
2:30remaining
What 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?
AIt cannot load MATLAB files saved in version 7.3 or later because they use HDF5 format.
BIt only loads files smaller than 1MB due to memory constraints.
CIt converts all numeric arrays to Python lists automatically.
DIt requires MATLAB to be installed on the system to work.
Attempts:
2 left
💡 Hint
Check the file format versions supported by scipy.io.loadmat.