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
np.save("scores.npy", a) # one array — exact, fast, losslessback = np.load("scores.npy")np.savez("data.npz", x=a, y=b) # several arrays in one filed = np.load("data.npz")d["x"], d["y"] # access by name
Text & CSV
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
- 01np.save/np.load use the .npy binary format — fast, lossless, dtype- and shape-preserving.
- 02np.savez bundles several arrays into one .npz, accessed by name.
- 03loadtxt/savetxt read and write numeric text/CSV.
- 04mmap_mode reads arrays larger than RAM in slices from disk.
