0
0
NumPydata~10 mins

np.savez() for multiple arrays in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.savez() for multiple arrays
Prepare arrays to save
Call np.savez(filename, arrays...)
Arrays packed into a .npz file
File saved on disk
Later: Load .npz file with np.load()
Access arrays by keys
Use arrays in your program
You prepare arrays, save them together in one .npz file, then load and access them later by name.
Execution Sample
NumPy
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

np.savez('data.npz', first=arr1, second=arr2)
This code saves two arrays named 'first' and 'second' into one file called 'data.npz'.
Execution Table
StepActionInput ArraysFile CreatedOutput
1Create arr1[1, 2, 3]Noarr1 ready
2Create arr2[4, 5, 6]Noarr2 ready
3Call np.savez('data.npz', first=arr1, second=arr2)[arr1, arr2]YesFile 'data.npz' with arrays saved
4Load file with np.load('data.npz')File 'data.npz'NoLoaded file object with keys 'first', 'second'
5Access arrays by keysLoaded fileNoArrays retrieved: first=[1 2 3], second=[4 5 6]
💡 All arrays saved and accessible from the .npz file.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
arr1None[1 2 3][1 2 3][1 2 3][1 2 3][1 2 3]
arr2NoneNone[4 5 6][4 5 6][4 5 6][4 5 6]
file 'data.npz'Not createdNot createdCreatedCreatedLoadedLoaded
loaded_fileNoneNoneNoneNoneFile objectFile object
loaded_file['first']NoneNoneNoneNoneNone[1 2 3]
loaded_file['second']NoneNoneNoneNoneNone[4 5 6]
Key Moments - 3 Insights
Why do we give names like 'first' and 'second' when saving arrays?
Because np.savez saves arrays with keys. These names become keys to access arrays later, as shown in execution_table step 5.
What happens if we don't give names to arrays in np.savez()?
Arrays get default names like 'arr_0', 'arr_1'. You can still access them by these keys after loading.
Is the .npz file human-readable?
No, it's a compressed binary file storing arrays efficiently. You must load it with np.load to read arrays.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is created?
AA new array in memory
BA text file with array data
CA file named 'data.npz' containing arrays
DAn error because arrays are unnamed
💡 Hint
Check the 'File Created' and 'Output' columns at step 3 in execution_table.
According to variable_tracker, what is the value of loaded_file['second'] after step 5?
A[4 5 6]
BNone
C[1 2 3]
DFile object
💡 Hint
Look at the last column for loaded_file['second'] in variable_tracker.
If we omit names in np.savez(arr1, arr2), how do we access the second array after loading?
Aloaded_file['second']
Bloaded_file['arr_1']
Cloaded_file[1]
Dloaded_file['arr2']
💡 Hint
Recall key naming defaults mentioned in key_moments about unnamed arrays.
Concept Snapshot
np.savez(filename, **arrays) saves multiple arrays in one .npz file.
Each array is saved with a key (name).
Load with np.load(filename) to get a file object.
Access arrays by keys like loaded['keyname'].
Useful to store and share multiple arrays together.
Full Transcript
This visual execution shows how np.savez saves multiple numpy arrays into a single .npz file. First, arrays are created in memory. Then np.savez is called with a filename and named arrays. This creates a compressed file storing all arrays with their keys. Later, np.load reads the file and returns an object to access arrays by their keys. The execution table traces each step from array creation, saving, loading, to accessing arrays. The variable tracker shows how variables and file states change. Key moments clarify why naming arrays is important and that .npz files are binary. The quiz tests understanding of file creation, array access, and default naming. The snapshot summarizes usage in a few lines.