savez_compressed() uses the same dict-like keyword-argument interface and .npz archive format as savez(), just applying zip compression to each array's data before writing — the result is loaded back identically with np.load(), with no code changes needed on the reading side. The trade-off is purely about time versus space: compression makes the resulting file noticeably smaller, especially for arrays with lots of repeated values or patterns, but takes longer to write, and can also take somewhat longer to read back, since the data must be decompressed.
1Understanding np.savez_compressed()
savez_compressed() uses the same dict-like keyword-argument interface and .npz archive format as savez(), just applying zip compression to each array's data before writing — the result is loaded back identically with np.load(), with no code changes needed on the reading side. The trade-off is purely about time versus space: compression makes the resulting file noticeably smaller, especially for arrays with lots of repeated values or patterns, but takes longer to write, and can also take somewhat longer to read back, since the data must be decompressed.
Reach for savez_compressed() instead of savez() when disk space or transfer size genuinely matters, like distributing a large dataset, but stick with the faster, uncompressed savez() for everyday intermediate files where saving/loading speed matters more than file size.
import numpy as np
large_array = np.zeros((1000, 1000))
np.savez_compressed("zeros.npz", data=large_array)
np.savez("zeros_uncompressed.npz", data=large_array)2Practical Example
Here is a real-world application of np.savez_compressed() showing how it is used in production NumPy code.
import numpy as np
arr = np.array([1, 2, 3])
np.savez_compressed("data.npz", values=arr)
loaded = np.load("data.npz")
print(loaded["values"])3Best Practices
Follow these guidelines when working with np.savez_compressed():
1. Use savez_compressed() for large arrays or datasets being stored long-term or distributed, where file size matters more than save/load speed
2. Use plain savez() for frequently-written intermediate results within a workflow, where save/load speed matters more than file size
3. Benchmark both if you're unsure — compression's benefit varies a lot depending on how repetitive or random the underlying data actually is
Tip: Reach for savez_compressed() instead of savez() when disk space or transfer size genuinely matters, like distributing a large dataset, but stick with the faster, uncompressed savez() for everyday intermediate files where saving/loading speed matters more than file size.
import numpy as np
large_array = np.zeros((1000, 1000))
np.savez_compressed("zeros.npz", data=large_array)
np.savez("zeros_uncompressed.npz", data=large_array)