The savez()
method is used to save multiple arrays into a single file in uncompressed .npz
format. It takes arrays as keyword arguments to save them into the specified file.
*args
, the savez()
method will save arrays to the argument file after the arr_0
, arr_1
, and so on.**kwds
, then savez()
will save arrays to the corresponding file as defined array names.numpy.savez(file, *args, **kwds)
file
: This can be a filename as a string or a file-like object.*args
: These are arrays as arguments to save in a specified file. Such arrays are saved in the file with names arr_0
, arr_1
, and so on.**kwds
: These are arrays as keyword arguments to save in a file with the keyword as the array name. It does not return anything.
# import numpy and tempfile modulesimport numpy as npimport tempfile as file# create a temporary file in local storageoutfile = file.TemporaryFile()# creating four random arraysx1 = np.random.randint(0, 20, 10)x2 = np.random.randint(0, 50, 10)x3 = np.random.randint(0, 100, 10)x4 = np.random.randint(10, 100, 10)# invoking savez() methodnp.savez(outfile, a = x1, b = x2, c = x3, d = x4)# Required to simulate the closing and reopening file_ = outfile.seek(0)# It'll load pickled objects or .npy, .npz file in programnpzfile = np.load(outfile)# print 'a' to 'd' arraysprint("a =", npzfile['a'])print("b =", npzfile['b'])print("c =", npzfile['c'])print("d =", npzfile['d'])
TemporaryFile()
method creates a temporary memory and returns a path-like object to outfile
.x1
, x2
, x3
, and x4
.np.savez(outfile, a = x1, b = x2, c = x3, d = x4)
command saves x1
, x2
, x3
, and x4
as a
, b
, c
, and d
in outfile
in .npz
format.outfile.seek(0)
.np.load()
to load the specified outfile
from memory to program. .npz
file.