save numpy array to file

创建日期: 2024-07-12 09:58 | 作者: 风波 | 浏览次数: 20 | 分类: Python

来源:https://numpy.org/doc/stable/reference/generated/numpy.savez.html#numpy.savez

numpy.savez

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

Save several arrays into a single file in uncompressed .npz format. rovide arrays as keyword arguments to store them under the corresponding 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 be arr_0, arr_1, etc.

Parameters:

Returns:

See also

Examples

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

np.savez(outfile, x, y)
npzfile = np.load(outfile)
npzfile.files  # ['arr_0', 'arr_1']
npzfile['arr_0']  # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

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])
20 浏览
14 爬虫
0 评论