NPZ archives keep related NumPy arrays together in one file, which suits small checkpoints, paired training data, and intermediate outputs that should be reloaded as a set. Unlike CSV or plain text exports, each member keeps NumPy shape and dtype metadata.
np.savez() writes an uncompressed archive, and np.savez_compressed() uses ZIP compression for smaller files when write speed is less important than file size. Passing keyword arguments gives each array a stable key, while positional arrays are loaded later as arr_0, arr_1, and similar generated names.
Loading an NPZ file returns a dictionary-like NpzFile object rather than one array. Use a context manager or close the loaded object after reading so the archive file handle is released, and keep allow_pickle=False for archives that do not need trusted object arrays.
Related: Save and load NPY
Related: Memory-map an NPY array
Related: Write CSV data
import numpy as np from pathlib import Path path = Path("model-batch.npz") features = np.arange(12, dtype=np.float32).reshape(3, 4) labels = np.array([0, 1, 1], dtype=np.int64) sample_ids = np.array(["train-001", "train-002", "train-003"]) np.savez_compressed( path, features=features, labels=labels, sample_ids=sample_ids, ) with np.load(path) as data: print("keys:", sorted(data.files)) print("features shape:", data["features"].shape) print("features dtype:", data["features"].dtype) print("labels:", data["labels"].tolist()) print("sample ids:", data["sample_ids"].tolist()) print("features match:", np.array_equal(features, data["features"])) print("labels match:", np.array_equal(labels, data["labels"])) print("file exists:", path.exists())
Keyword names passed to np.savez_compressed() become archive keys. Use np.savez() with the same keyword pattern when write speed matters more than file size.
$ python array-save-load-npz.py keys: ['features', 'labels', 'sample_ids'] features shape: (3, 4) features dtype: float32 labels: [0, 1, 1] sample ids: ['train-001', 'train-002', 'train-003'] features match: True labels match: True file exists: True
The with np.load(path) block closes the NpzFile handle after the arrays are read.
$ rm model-batch.npz array-save-load-npz.py
Keep the NPZ file instead when it is the checkpoint another job should consume.