How to Use np.savez for Multiple Arrays in NumPy
Use
np.savez to save multiple arrays into one compressed file by passing arrays as keyword arguments or positional arguments. Each array can be accessed later by its name when loading the file with np.load. This method helps organize and store multiple arrays efficiently in one file.Syntax
The basic syntax of np.savez is:
np.savez(file, *args, **kwargs)
Where:
file: The filename or file object to save the arrays to (usually ends with.npz).*args: Arrays passed without names, saved with default names likearr_0,arr_1, etc.**kwargs: Arrays passed with names, saved with those names for easy access later.
python
np.savez(file, *args, **kwargs)
Example
This example shows how to save three arrays into one file using np.savez with named arrays, then load and access them.
python
import numpy as np # Create sample arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) array3 = np.array([7, 8, 9]) # Save arrays with names into a .npz file np.savez('arrays.npz', first=array1, second=array2, third=array3) # Load the .npz file loaded = np.load('arrays.npz') # Access arrays by their names first_array = loaded['first'] second_array = loaded['second'] third_array = loaded['third'] print('First array:', first_array) print('Second array:', second_array) print('Third array:', third_array)
Output
First array: [1 2 3]
Second array: [4 5 6]
Third array: [7 8 9]
Common Pitfalls
Common mistakes when using np.savez include:
- Not using keyword arguments, which causes arrays to be saved with default names like
arr_0, making it harder to remember which is which. - Forgetting to add the
.npzextension, which can confuse file handling. - Trying to save non-array objects, which
np.savezdoes not support.
Always use keyword arguments for clarity and include the .npz extension in the filename.
python
import numpy as np # Wrong way: positional arguments without names array1 = np.array([1, 2]) array2 = np.array([3, 4]) np.savez('wrong_save.npz', array1, array2) loaded_wrong = np.load('wrong_save.npz') print('Default names:', loaded_wrong.files) # Right way: keyword arguments with names np.savez('right_save.npz', first=array1, second=array2) loaded_right = np.load('right_save.npz') print('Named arrays:', loaded_right.files)
Output
Default names: ['arr_0', 'arr_1']
Named arrays: ['first', 'second']
Quick Reference
Tips for using np.savez effectively:
- Use keyword arguments to name arrays for easy access.
- Always use the
.npzfile extension. - Load saved files with
np.loadand access arrays by their names. - Use
np.savez_compressedif you want to save space with compression.
Key Takeaways
Use np.savez with keyword arguments to save multiple arrays with names in one file.
Always include the .npz extension when saving files with np.savez.
Load arrays with np.load and access them by the names used during saving.
Positional arguments save arrays with default names like arr_0, which can be confusing.
For smaller file size, consider using np.savez_compressed instead of np.savez.