🚀 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.load()

AI & DATA SCIENCE // np-load

np.load() reads an array (or a dict-like collection of arrays) back from a file previously saved with np.save(), np.savez(), or np.savez_compressed().

Syntax

np.load(file, allow_pickle=False)

Deep Dive Course

np.load() inspects the file to determine what it actually contains: loading a .npy file returns a single ndarray directly, while loading a .npz file, whether compressed or not, returns a dict-like NpzFile object exposing each saved array by the name it was given. By default, allow_pickle=False refuses to load files containing pickled Python objects, as opposed to plain numeric array data, a deliberate security measure, since unpickling data from an untrusted source can execute arbitrary code.

1Understanding np.load()

np.load() inspects the file to determine what it actually contains: loading a .npy file returns a single ndarray directly, while loading a .npz file, whether compressed or not, returns a dict-like NpzFile object exposing each saved array by the name it was given. By default, allow_pickle=False refuses to load files containing pickled Python objects, as opposed to plain numeric array data, a deliberate security measure, since unpickling data from an untrusted source can execute arbitrary code.

💡

Only set allow_pickle=True when you specifically trust the source of the file and know it needs to contain pickled Python objects — loading a file with pickling enabled from an untrusted source is a genuine security risk, since unpickling can execute arbitrary code.

editor.html
import numpy as np

arr = np.array([10, 20, 30])
np.save("numbers.npy", arr)
loaded = np.load("numbers.npy")
print(loaded)
print(type(loaded))
localhost:3000

2Practical Example

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

editor.html
import numpy as np

np.savez("bundle.npz", a=np.array([1, 2]), b=np.array([3, 4]))
with np.load("bundle.npz") as data:
    print(data["a"])
    print(data["b"])
localhost:3000

3Best Practices

Follow these guidelines when working with np.load():

1. Leave allow_pickle at its default of False unless you specifically know the file needs it, to avoid a real security risk from untrusted files

2. Check whether a loaded object is a plain ndarray, from .npy, or an NpzFile, from .npz, if your code needs to handle both possibilities

3. Close an NpzFile object explicitly, or load it inside a with block, once you're done reading from it, the same as you would for a regular file

⚠️

Tip: Only set allow_pickle=True when you specifically trust the source of the file and know it needs to contain pickled Python objects — loading a file with pickling enabled from an untrusted source is a genuine security risk, since unpickling can execute arbitrary code.

editor.html
import numpy as np

arr = np.array([10, 20, 30])
np.save("numbers.npy", arr)
loaded = np.load("numbers.npy")
print(loaded)
print(type(loaded))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([10, 20, 30])
np.save("numbers.npy", arr)
loaded = np.load("numbers.npy")
print(loaded)
print(type(loaded))
Example 02Advanced Example
import numpy as np

np.savez("bundle.npz", a=np.array([1, 2]), b=np.array([3, 4]))
with np.load("bundle.npz") as data:
    print(data["a"])
    print(data["b"])

Best Practices

  • Leave allow_pickle at its default of False unless you specifically know the file needs it, to avoid a real security risk from untrusted files
  • Check whether a loaded object is a plain ndarray, from .npy, or an NpzFile, from .npz, if your code needs to handle both possibilities
  • Close an NpzFile object explicitly, or load it inside a with block, once you're done reading from it, the same as you would for a regular file

Interview Question

Why does np.load() refuse to load certain files by default unless you explicitly pass allow_pickle=True?

Hint: Think about what pickling actually does and the risk of loading data from an untrusted source.

Pickle is Python's general object-serialization format, and unpickling data doesn't just parse plain values — it can execute arbitrary code as part of reconstructing certain kinds of objects, which makes loading a pickled file from an untrusted or unverified source a genuine security risk, similar to running code you haven't reviewed. NumPy's default of allow_pickle=False blocks that risk for the common case of loading straightforward numeric array data, requiring you to explicitly opt in with allow_pickle=True only when you specifically trust the file's origin and know it needs to contain pickled objects, like arrays of arbitrary Python objects rather than plain numbers.

Exercises

MediumPractice using np.load() in a real scenario.
View Solution
import numpy as np

arr = np.array([10, 20, 30])
np.save("numbers.npy", arr)
loaded = np.load("numbers.npy")
print(loaded)
print(type(loaded))

Frequently Asked Questions

Why does np.load() refuse to load certain files by default unless you explicitly pass allow_pickle=True?

Pickle is Python's general object-serialization format, and unpickling data doesn't just parse plain values — it can execute arbitrary code as part of reconstructing certain kinds of objects, which makes loading a pickled file from an untrusted or unverified source a genuine security risk, similar to running code you haven't reviewed. NumPy's default of allow_pickle=False blocks that risk for the common case of loading straightforward numeric array data, requiring you to explicitly opt in with allow_pickle=True only when you specifically trust the file's origin and know it needs to contain pickled objects, like arrays of arbitrary Python objects rather than plain numbers.

Related Functions

np-savenp-savezwith-statement