Passing arrays as keyword arguments to savez() associates each one with a name, which is exactly how you retrieve individual arrays back out after loading the archive — np.load() on an .npz file returns a dict-like object where each saved array is accessed by the name it was given. Internally, an .npz file is just a zip archive containing one .npy file per saved array, bundled together for convenience.
1Understanding np.savez()
Passing arrays as keyword arguments to savez() associates each one with a name, which is exactly how you retrieve individual arrays back out after loading the archive — np.load() on an .npz file returns a dict-like object where each saved array is accessed by the name it was given. Internally, an .npz file is just a zip archive containing one .npy file per saved array, bundled together for convenience.
Always save arrays into an .npz archive with meaningful keyword names rather than positional arguments — positional arrays get generic auto-generated names like arr_0, arr_1, which are much less clear when loading them back later.
import numpy as np
features = np.array([[1, 2], [3, 4]])
labels = np.array([0, 1])
np.savez("dataset.npz", features=features, labels=labels)
data = np.load("dataset.npz")
print(data["features"])
print(data["labels"])2Practical Example
Here is a real-world application of np.savez() showing how it is used in production NumPy code.
import numpy as np
np.savez("dataset.npz", features=np.zeros((2, 2)), labels=np.ones(2))
data = np.load("dataset.npz")
print(list(data.keys()))3Best Practices
Follow these guidelines when working with np.savez():
1. Use keyword arguments when calling savez() so saved arrays have meaningful names, rather than the auto-generated arr_0, arr_1 style names positional arguments produce
2. Bundle related arrays that are always used together, like training features and labels, into one .npz file, instead of managing several separate .npy files
3. Remember the object returned by np.load() for an .npz file behaves like a dict and should typically be closed, or used in a with block, when done
Tip: Always save arrays into an .npz archive with meaningful keyword names rather than positional arguments — positional arrays get generic auto-generated names like arr_0, arr_1, which are much less clear when loading them back later.
import numpy as np
features = np.array([[1, 2], [3, 4]])
labels = np.array([0, 1])
np.savez("dataset.npz", features=features, labels=labels)
data = np.load("dataset.npz")
print(data["features"])
print(data["labels"])