Overview
- The savez() and savez_compressed() functions of the Python NumPy module write multiple ndarray objects into a disk file.
- While savez() writes the ndarray objects in uncompressed form, the savez_compressed() writes the arrays in compressed form.
- For both savez() and savez_compressed(), when no file name extension is given .npz is assumed as the extension of the file.
- Unlike savez() and savez_compressed() functions the save() function writes only one ndarray per disk file.
Example
# Example Python program that uses numpy.savez()
# function to save multiple arrays into a file.
import numpy as np
# Create a two-dimensional array
a1 = np.ones((2, 2), dtype = np.int8)
for x in range (0, 1):
for y in range (0, 1):
a1[x][y] = x * y
# Create a three-dimensional array
a2 = np.ones((3, 3, 3), dtype = np.int8)
for x in range (0, 3):
for y in range (0, 3):
for z in range (0, 3):
a2[x][y][z] = x * y * z
# Write both the arrays into a .npz file - Uncompressed form
np.savez("multi_array_uncompressed.npz", array1 = a1, array2 = a2)
# Write both the arrays into a .npz file - Compressed form
np.savez_compressed("multi_array_compressed.npz", array1 = a1, array2 = a2)Comparing the size of .npz files - compressed vs uncompressed
The compressed version saves the overhead of 129 bytes when compared to the uncompressed version.
-rw-r--r-- 1 root u1 412 Aug 22 14:29 multi_array_compressed.npz
-rw-r--r-- 1 root u1 541 Aug 22 14:29 multi_array_uncompressed.npzContents of the uncompressed file
PK^C^D-^@^@^@^@^@^@^@!^@ ¯F´ÿÿÿÿÿÿÿÿ
^@^T^@array1.npy^A^@^P^@<84>^@^@^@^@^@^@^@<84>^@^@^@^@^@^@^@<93>NUMPY^A^@v^@{'descr': '|i1', 'fortran_order': False, 'shape': (2, 2), }
^@^A^A^APK^C^D-^@^@^@^@^@^@^@!^@À¾<95>·ÿÿÿÿÿÿÿÿ
^@^T^@array2.npy^A^@^P^@<9b>^@^@^@^@^@^@^@<9b>^@^@^@^@^@^@^@<93>NUMPY^A^@v^@{'descr': '|i1', 'fortran_order': False, 'shape': (3, 3, 3), }
^@^@^@^@^@^@^@^@^@^@^@^@^@^A^B^@^B^D^@^@^@^@^B^D^@^D^HPK^A^B-^C-^@^@^@^@^@^@^@!^@ ¯F´<84>^@^@^@<84>^@^@^@
^@^@^@^@^@^@^@^@^@^@^@<80>^A^@^@^@^@array1.npyPK^A^B-^C-^@^@^@^@^@^@^@!^@À¾<95>·<9b>^@^@^@<9b>^@^@^@
^@^@^@^@^@^@^@^@^@^@^@<80>^AÀ^@^@^@array2.npyPK^E^F^@^@^@^@^B^@^B^@p^@^@^@<97>^A^@^@^@^@Contents of the compressed file
The file is opened using the vi editor.
sudo vi multi_array_compressed.npzThe savez_compressed() function writes everything in compressed form including the header information.
PK^C^D-^@^@^@^H^@^@^@!^@ ¯F´ÿÿÿÿÿÿÿÿ
^@^T^@array1.npy^A^@^P^@<84>^@^@^@^@^@^@^@J^@^@^@^@^@^@^@<9b>ì^Wê^[^PÉÈPÆP<9e><92>Z<9c>\¤n¥ ^<93>i¨®£ <9e><96>_TR<94><98>^W<9f>_<94><92>
^RwKÌ)N^E<8a>^Wg$^V¤^Bù^ZF:
F<9a>:
µ
d^C.^FFFF^@PK^C^D-^@^@^@^H^@^@^@!^@À¾<95>·ÿÿÿÿÿÿÿÿ
^@^T^@array2.npy^A^@^P^@<9b>^@^@^@^@^@^@^@T^@^@^@^@^@^@^@<9b>ì^Wê^[^PÉÈPÆP<9e><92>Z<9c>\¤n¥ ^<93>i¨®£ <9e><96>_TR<94><98>^W<9f>_<94><92>
^RwKÌ)N^E<8a>^Wg$^V¤^Bù^ZÆ:
¤©£P«@^^àb@^F<8c>L^LL, ^F<90>dá^@^@PK^A^B-^C-^@^@^@^H^@^@^@!^@ ¯F´J^@^@^@<84>^@^@^@
^@^@^@^@^@^@^@^@^@^@^@<80>^A^@^@^@^@array1.npyPK^A^B-^C-^@^@^@^H^@^@^@!^@À¾<95>·T^@^@^@<9b>^@^@^@
^@^@^@^@^@^@^@^@^@^@^@<80>^A<86>^@^@^@array2.npyPK^E^F^@^@^@^@^B^@^B^@p^@^@^@^V^A^@^@^@^@