Introduction
An .npy file holds one array saved by NumPy,
the array library for Python, with numpy.save. It stores the array’s
shape, element type and byte order in a short text header, then the raw values, so it
loads quickly and can be memory-mapped without reading the whole file.
An .npz file holds several arrays: it is a
ZIP archive of .npy files, one per array,
written by numpy.savez or, compressed, by
numpy.savez_compressed.
Structure
The file starts with the magic string \x93NUMPY (the byte
93 and the letters NUMPY), then two bytes for the major and
minor version of the format, then the length of the header: 2 bytes, little-endian, in
version 1.0, and 4 bytes in versions 2.0 and 3.0.
The header is a Python dictionary written as text, for example
{'descr': '<f8', 'fortran_order': False, 'shape': (3, 4), }. The
descr gives the element type and byte order, fortran_order
says whether the array is stored column by column, and shape gives its
dimensions. The header is padded with spaces and ends with a line feed, so that the
data starts on a multiple of 64 bytes. The array’s values follow with nothing after
them.
0Magic\x93NUMPY6 bytes6Version major and minor2 bytes8Header length2 or 4 bytes…Header descr, fortran_order and shape as text, paddedvaries…Array data the raw valuesvaries
Links
Software
- Python with NumPy (Windows, macOS, Linux)
Developers
- numpy.load and
numpy.save, Python - cnpy, C++
Reference
- numpy.lib.format, the NumPy documentation of the format