np.save/load
np.save writes an array to a compact binary .npy file and np.load reads it back exactly as it was.
In this page:
Syntax
np.save("file.npy", arr)
arr = np.load("file.npy")
np.save/load
The .npy format stores the array with its dtype and shape, so nothing is lost on reload. np.savez bundles several arrays into one .npz file. Binary files are smaller and faster than text but not human-readable.
Note:
np.save adds the .npy extension for you if you leave it off.
Example: np.save/load
import numpy as np
import os
import tempfile
a = np.arange(6).reshape(2, 3)
path = os.path.join(tempfile.mkdtemp(), "data.npy")
np.save(path, a)
b = np.load(path)
print(b)
print("identical:", np.array_equal(a, b))
# Output:
# [[0 1 2]
# [3 4 5]]
# identical: True
Related Topics
Common Mistakes
- Expecting a .npy file to be readable in a text editor
- Forgetting allow_pickle for object arrays
- Overwriting an existing file unintentionally
Chapter Summary
- save writes a .npy binary file
- load restores dtype and shape
- savez stores several arrays
- Not human-readable
🔒
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: