🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.savez()

AI & DATA SCIENCE // np-savez

np.savez() saves multiple named arrays together into a single, uncompressed .npz archive file.

Syntax

np.savez(file, name1=arr1, name2=arr2, ...)

Deep Dive Course

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.

editor.html
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"])
localhost:3000

2Practical Example

Here is a real-world application of np.savez() showing how it is used in production NumPy code.

editor.html
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()))
localhost:3000

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.

editor.html
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"])
localhost:3000

Examples

Example 01Basic Usage
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"])
Example 02Advanced Example
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()))

Best Practices

  • 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
  • Bundle related arrays that are always used together, like training features and labels, into one .npz file, instead of managing several separate .npy files
  • 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

Interview Question

How do you retrieve a specific array back out of an .npz file after loading it with np.load()?

Hint: Think about the type of object np.load() returns for an .npz file, and how the arrays were originally named.

np.load() on an .npz file returns a special dict-like object, an NpzFile, where each key is the name each array was given as a keyword argument when it was saved with savez(). You retrieve a specific array by indexing that object with the matching key name, the same way you'd look up a value in a regular Python dictionary, and you can also call .keys() on it to see the names of every array stored in the archive.

Exercises

MediumPractice using np.savez() in a real scenario.
View Solution
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"])

Frequently Asked Questions

How do you retrieve a specific array back out of an .npz file after loading it with np.load()?

np.load() on an .npz file returns a special dict-like object, an NpzFile, where each key is the name each array was given as a keyword argument when it was saved with savez(). You retrieve a specific array by indexing that object with the matching key name, the same way you'd look up a value in a regular Python dictionary, and you can also call .keys() on it to see the names of every array stored in the archive.

Related Functions

np-savenp-savez-compressednp-load