w3resource

NumPy Input and Output: savez() function

numpy.savez() function

The savez() function is used to save several arrays into a single file in uncompressed .npz format.

If arguments are passed in with no keywords, the corresponding variable names, in the .npz file, are ‘arr_0’, ‘arr_1’, etc.
If keyword arguments are given, the corresponding variable names, in the .npz file will match the keyword names.

Syntax:

numpy.savez(file, *args, **kwds)

Version: 1.15.0

Parameter:

Name Description Required /
Optional
file Either the file name (string) or an open file (file-like object) where the data will be saved.
If file is a string or a Path, the .npz extension will be appended to the file name if it is not already there.
str or file
Required
args Arrays to save to the file. Since it is not possible for Python to know the names of the arrays outside savez,
the arrays will be saved with names “arr_0”, “arr_1”, and so on. These arguments can be any expression.
Arguments
Optional
kwds Arrays to save to the file. Arrays will be saved in the file with the keyword names.
Keyword arguments
Optional

Returns: None

Notes:
The .npz file format is a zipped archive of files named after the variables they contain.
The archive is not compressed and each file in the archive contains one variable in .npy format.

When opening the saved .npz file with load a NpzFile object is returned.
This is a dictionary-like object which can be queried for its list of arrays (with the .files attribute),and for the arrays themselves.

NumPy.savez() method Example-1:

Using savez with *args, the arrays are saved with default names.

>>> import numpy as np
>>> from tempfile import TemporaryFile
>>> outfile = TemporaryFile()
>>> a = np.arange(12)
>>> b = np.sin(a)
>>> np.savez(outfile, a, b)
>>> outfile.seek(0) # Only needed here to simulate closing & reopening file
>>> npzfile = np.load(outfile)
>>> npzfile.files

Output:

['arr_1', 'arr_0']
>>> import numpy as np
>>> from tempfile import TemporaryFile
>>> outfile = TemporaryFile()
>>> a = np.arange(12)
>>> b = np.sin(a)
>>> np.savez(outfile, a, b)
>>> outfile.seek(0) # Only needed here to simulate closing & reopening file
>>> npzfile = np.load(outfile)
>>> npzfile['arr_0']

Output:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
>>> import numpy as np
>>> from tempfile import TemporaryFile
>>> outfile = TemporaryFile()
>>> a = np.arange(12)
>>> b = np.sin(a)
>>> np.savez(outfile, a, b)
>>> outfile.seek(0) # Only needed here to simulate closing & reopening file
>>> npzfile = np.load(outfile)
>>> npzfile['arr_1']

Output:

array([ 0.        ,  0.84147098,  0.90929743,  0.14112001, -0.7568025 ,
       -0.95892427, -0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849,
       -0.54402111, -0.99999021])

NumPy.savez() method Example-2:

Using savez with **kwds, the arrays are saved with the keyword names.

>>> import numpy as np
>>> from tempfile import TemporaryFile
>>> outfile = TemporaryFile()
>>> a = np.arange(12)
>>> b = np.sin(a)
>>> outfile = TemporaryFile()
>>> np.savez(outfile, a=a, b=b)
>>> outfile.seek(0)
>>> npzfile = np.load(outfile)
>>> npzfile.files

Output:

['a', 'b']
>>> import numpy as np
>>> from tempfile import TemporaryFile
>>> outfile = TemporaryFile()
>>> a = np.arange(12)
>>> b = np.sin(a)
>>> outfile = TemporaryFile()
>>> np.savez(outfile, a=a, b=b)
>>> outfile.seek(0)
>>> npzfile = np.load(outfile)
>>> npzfile['a']

Output:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
>>> import numpy as np
>>> from tempfile import TemporaryFile
>>> outfile = TemporaryFile()
>>> a = np.arange(12)
>>> b = np.sin(a)
>>> outfile = TemporaryFile()
>>> np.savez(outfile, a=a, b=b)
>>> outfile.seek(0)
>>> npzfile = np.load(outfile)
>>> npzfile['b']

Output:

array([ 0.        ,  0.84147098,  0.90929743,  0.14112001, -0.7568025 ,
       -0.95892427, -0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849,
       -0.54402111, -0.99999021])

Python - NumPy Code Editor:

Previous: save() function
Next: savez_compressed() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/numpy/input-and-output/savez.php