Skip to main content
Albero Academy

Saving & Loading

Persist arrays with .npy/.npz and read numeric text and CSV.

Applied NumPy8 min

Arrays need to leave memory — saved between runs, shared, or loaded from a data file. NumPy has a fast native binary format and readers for text data.

The .npy / .npz formats

native binary I/O
np.save("scores.npy", a) # one array — exact, fast, lossless
back = np.load("scores.npy")
np.savez("data.npz", x=a, y=b) # several arrays in one file
d = np.load("data.npz")
d["x"], d["y"] # access by name

Text & CSV

reading & writing text
data = np.loadtxt("marks.csv", delimiter=",", skiprows=1)
np.savetxt("out.csv", a, delimiter=",", fmt="%.2f")
big = np.load("huge.npy", mmap_mode="r") # memory-map data bigger than RAM

Key takeaways

  1. 01np.save/np.load use the .npy binary format — fast, lossless, dtype- and shape-preserving.
  2. 02np.savez bundles several arrays into one .npz, accessed by name.
  3. 03loadtxt/savetxt read and write numeric text/CSV.
  4. 04mmap_mode reads arrays larger than RAM in slices from disk.

Finished this chapter?

Mark it complete to track your progress through NumPy.