numpy.savez

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

Save several arrays into a single, compressed file in .npz format.

If keyword arguments are given, the names for variables assigned to the keywords are the keyword names (not the variable names in the caller). If arguments are passed in with no keywords, the corresponding variable names are arr_0, arr_1, etc.

Parameters:

file : str or file

Either the file name (string) or an open file (file-like object) If file is a string, it names the output file. “.npz” will be appended to the file name if it is not already there.

args : Arguments

Any function arguments other than the file name are variables to save. Since it is not possible for Python to know their names outside savez, they will be saved with names “arr_0”, “arr_1”, and so on. These arguments can be any expression.

kwds : Keyword arguments

All keyword=value pairs cause the value to be saved with the name of the keyword.

See also

save
Save a single array to a binary file in NumPy format.
savetxt
Save an array to a file as plain text.

Notes

The .npz file format is a zipped archive of files named after the variables they contain. Each file contains one variable in .npy format. For a description of the .npy format, see format.

Examples

>>> from tempfile import TemporaryFile
>>> outfile = TemporaryFile()
>>> x = np.arange(10)
>>> y = np.sin(x)

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

>>> np.savez(outfile, x, y)
>>> outfile.seek(0)  # only necessary in this example (with tempfile)
>>> npz = np.load(outfile)
>>> npz.files
['arr_1', 'arr_0']
>>> npz['arr_0']
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

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

>>> outfile = TemporaryFile()
>>> np.savez(outfile, x=x, y=y)
>>> outfile.seek(0)
>>> npz = np.load(outfile)
>>> npz.files
['y', 'x']
>>> npz['x']
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Previous topic

numpy.save

Next topic

numpy.loadtxt

This Page