A great question about NumPy's data serialization functions!
np.savez and np.savez_compressed are both used to save multiple NumPy arrays to a single file, but they differ in how they handle the data storage:
np.savez:
- Saves arrays uncompressed in a ZIP archive (yes, it's a ZIP file, even though the extension is
.npz).
- Each array is stored as a separate file within the ZIP archive, named
arr_0.npy, arr_1.npy, etc.
- Fast write and read times, as no compression is involved.
- Larger file sizes, since data is not compressed.
np.savez_compressed:
- Saves arrays compressed in a ZIP archive (again, a ZIP file with a
.npz extension).
- Each array is stored as a separate file within the ZIP archive, just like with
np.savez, but compressed using DEFLATE (the same algorithm used in gzip, zip, and zlib).
- Slower write times due to the compression step, but read times are still relatively fast.
- Smaller file sizes, thanks to compression.
Here are some guidelines to help you decide when to use each:
Use np.savez when:
- Speed is crucial: You need to save or load large arrays quickly, and storage space is not a concern.
- Data is already compressed: If your arrays contain data that's already compressed (e.g., images, audio), further compression won't gain much.
- Random access is necessary: Since each array is stored separately, you can load individual arrays from the
.npz file without loading the entire file into memory (using np.load(..., mmap_mode='r')).
- Debugging or inspecting the data: Uncompressed
.npz files are easier to inspect, as you can simply unzip the file and explore the individual .npy files.
Use np.savez_compressed when:
- Storage space is limited: You need to store a large number of arrays or very large arrays, and disk space is a concern.
- Data compresses well: If your arrays contain repetitive or sparse data (e.g., scientific simulations, statistical data), compression can significantly reduce the file size.
- Data is archived for long-term storage: Compressed files are more suitable for long-term archiving, as they'll consume less storage space over time.
- Network transfer is involved: Smaller compressed files transfer faster over networks, making
np.savez_compressed a good choice for distributing data.
To give you a rough idea, here's a simple benchmark:
import numpy as np
import time
# Create a large array
arr = np.random.rand(1000, 1000)
# savez
start = time.time()
np.savez('uncompressed.npz', arr)
print("np.savez took:", time.time() - start, "seconds")
# Output: np.savez took: 0.045 seconds, file size: 7.6 MB
# savez_compressed
start = time.time()
np.savez_compressed('compressed.npz', arr)
print("np.savez_compressed took:", time.time() - start, "seconds")
# Output: np.savez_compressed took: 0.234 seconds, file size: 3.4 MB
In this example, np.savez_compressed took about 5 times longer to write, but the file size was reduced by approximately 55%.
In summary:
- If speed and random access are more important than storage space, choose
np.savez.
- If storage efficiency and smaller file sizes are prioritized, and you can afford a bit more processing time, choose
np.savez_compressed.
Hope this helps you make informed decisions about your NumPy data serialization!