numpy.savez#
- numpy.savez(file,*args,allow_pickle=True,**kwds)[source]#
Save several arrays into a single file in uncompressed
.npzformat.Provide arrays as keyword arguments to store them under thecorresponding name in the output file:
savez(fn,x=x,y=y).If arrays are specified as positional arguments, i.e.,
savez(fn,x,y), their names will bearr_0,arr_1, etc.- Parameters:
- filefile, str, or pathlib.Path
Either the filename (string) or an open file (file-like object)where the data will be saved. If file is a string or a Path, the
.npzextension will be appended to the filename if it is notalready there.- argsArguments, optional
Arrays to save to the file. Please use keyword arguments (seekwds below) to assign names to arrays. Arrays specified asargs will be named “arr_0”, “arr_1”, and so on.
- allow_picklebool, optional
Allow saving object arrays using Python pickles. Reasons fordisallowing pickles include security (loading pickled data can executearbitrary code) and portability (pickled objects may not be loadableon different Python installations, for example if the stored objectsrequire libraries that are not available, and not all pickled data iscompatible between different versions of Python).Default: True
- kwdsKeyword arguments, optional
Arrays to save to the file. Each array will be saved to theoutput file with its corresponding keyword name.
- Returns:
- None
See also
saveSave a single array to a binary file in NumPy format.
savetxtSave an array to a file as plain text.
savez_compressedSave several arrays into a compressed
.npzarchive
Notes
The
.npzfile format is a zipped archive of files named after thevariables they contain. The archive is not compressed and each filein the archive contains one variable in.npyformat. For adescription of the.npyformat, seenumpy.lib.format.When opening the saved
.npzfile withloadaNpzFileobject is returned. This is a dictionary-like object which can be queriedfor its list of arrays (with the.filesattribute), and for the arraysthemselves.Keys passed inkwds are used as filenames inside the ZIP archive.Therefore, keys should be valid filenames; e.g., avoid keys that begin with
/or contain..When naming variables with keyword arguments, it is not possible to name avariable
file, as this would cause thefileargument to be definedtwice in the call tosavez.Examples
>>>importnumpyasnp>>>fromtempfileimportTemporaryFile>>>outfile=TemporaryFile()>>>x=np.arange(10)>>>y=np.sin(x)
Using
savezwith *args, the arrays are saved with default names.>>>np.savez(outfile,x,y)>>>_=outfile.seek(0)# Only needed to simulate closing & reopening file>>>npzfile=np.load(outfile)>>>npzfile.files['arr_0', 'arr_1']>>>npzfile['arr_0']array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Using
savezwith **kwds, the arrays are saved with the keyword names.>>>outfile=TemporaryFile()>>>np.savez(outfile,x=x,y=y)>>>_=outfile.seek(0)>>>npzfile=np.load(outfile)>>>sorted(npzfile.files)['x', 'y']>>>npzfile['x']array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])