0
0
SciPydata~10 mins

Saving and loading data (scipy.io) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Saving and loading data (scipy.io)
Create data arrays
Use scipy.io.savemat to save data to .mat file
Use scipy.io.loadmat to read data from .mat file
Access loaded data arrays
Use data for analysis or visualization
The flow shows creating data, saving it to a file, loading it back, and then using the loaded data.
Execution Sample
SciPy
import numpy as np
from scipy.io import savemat, loadmat

# Create data
data = {'x': np.array([1,2,3]), 'y': np.array([4,5,6])}

# Save data
savemat('datafile.mat', data)

# Load data
loaded = loadmat('datafile.mat', squeeze_me=True)
print(loaded['x'])
This code saves two arrays to a .mat file and then loads them back, printing one array.
Execution Table
StepActionData StateResult/Output
1Create dictionary with arrays 'x' and 'y'{'x': [1 2 3], 'y': [4 5 6]}Data ready to save
2Call savemat('datafile.mat', data)File 'datafile.mat' createdData saved to file
3Call loadmat('datafile.mat', squeeze_me=True)Read file contentDictionary with keys including 'x', 'y', '__header__', '__version__', '__globals__'
4Access loaded['x']Extract array for 'x'[1 2 3]
5Print loaded['x']Output to console[1 2 3]
6End of scriptNo changeExecution stops
💡 Script ends after printing loaded data array 'x'
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
data{}{'x': array([1, 2, 3]), 'y': array([4, 5, 6])}SameSameSameSame
loadedN/AN/AN/A{'__header__': ..., '__version__': ..., '__globals__': ..., 'x': array([1, 2, 3]), 'y': array([4, 5, 6])}SameSame
Key Moments - 2 Insights
Why does the loaded dictionary have extra keys like '__header__' besides 'x' and 'y'?
When loading a .mat file, scipy.io.loadmat returns a dictionary with metadata keys like '__header__', '__version__', and '__globals__' along with your saved variables. See execution_table step 3.
Can I save variables other than arrays using savemat?
savemat saves variables as arrays or compatible types. Complex Python objects may not save correctly. Use arrays or simple data structures as in execution_table step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of loaded['x']?
A[4 5 6]
B[1 2 3]
CA dictionary
DAn error
💡 Hint
Check the 'Result/Output' column at step 4 in execution_table.
At which step is the .mat file created?
AStep 1
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Action' and 'Result/Output' columns for file creation in execution_table.
If you change the data dictionary to include a string, what might happen when saving?
AIt raises an error or saves incorrectly
BIt saves without issues
CIt converts string to array automatically
DIt ignores the string variable
💡 Hint
Refer to key_moments about data types savemat can handle.
Concept Snapshot
Saving and loading data with scipy.io:
- Use savemat(filename, dict) to save arrays to .mat file
- Use loadmat(filename) to load data back as a dictionary
- Loaded dict includes metadata keys plus your variables
- Save only arrays or compatible types
- Access variables by their keys after loading
Full Transcript
This visual execution shows how to save and load data using scipy.io. First, we create a dictionary with numpy arrays. Then, we save it to a .mat file using savemat. Next, we load the file back with loadmat, which returns a dictionary including metadata and our saved arrays. We access the arrays by their keys and print them. The variable tracker shows how 'data' and 'loaded' change. Key moments clarify why extra keys appear and data type limits. The quiz tests understanding of loaded data, file creation step, and data type handling.