Python Central HubSign in

Data Analytics with Python / Phase 2: Numerical Computing NumPy

Saving and Loading NumPy Data

Save NumPy arrays to disk with .npy/.npz, load them back efficiently, and exchange data using text/CSV formats.

Why save NumPy arrays?

Saving arrays lets you:

  • reuse cleaned/processed features
  • speed up workflows (avoid re-parsing CSV)
  • share data between scripts/notebooks

NumPy's own binary format is .npy (one array) / .npz (multiple arrays) โ€” uncompressed by default, and much faster to reload than re-parsing text.

Save and load single array: .npy

Save

Python
import numpy as np
 
arr = np.arange(10)
np.save("my_array.npy", arr)

Load

Python
import numpy as np
 
arr2 = np.load("my_array.npy")
print(arr2)
diagram Choosing a NumPy save format mermaid
Pick .npy/.npz for fast, exact round-trips of array data; use text formats only for simple, human-readable numeric matrices.

Save and load multiple arrays: .npz

If the file path doesn't end in .npy/.npz, NumPy appends the extension automatically.

Python
import numpy as np
 
x = np.arange(5)
y = np.arange(5) ** 2
 
np.savez("data.npz", x=x, y=y)
 
loaded = np.load("data.npz")
print(loaded.files)
print(loaded["x"], loaded["y"])

If the saved data compresses well, np.savez_compressed produces a smaller file at the cost of a bit more CPU time:

Python
import numpy as np
 
arr = np.arange(10)
np.savez_compressed("arrays_compressed.npz", a=arr, b=arr)

Text formats (CSV-like)

Save to text

Python
import numpy as np
 
mat = np.array([[1.1, 2.2], [3.3, 4.4]])
np.savetxt("matrix.csv", mat, delimiter=",", fmt="%.2f")

Load from text

Python
import numpy as np
 
mat2 = np.loadtxt("matrix.csv", delimiter=",")
print(mat2)

Tips

  • For best speed and exact dtype preservation: use .npy / .npz.
  • Use .npz when you want one file containing multiple arrays.

Check your understanding

quizCheck yourself
  1. Which file extension does NumPy use for a single array saved with np.save, and which for several arrays saved with np.savez?

    Show answer

    D โ€” .npy for a single array and .npz for several โ€” np.save writes one array to a .npy file. np.savez bundles several named arrays into a .npz file.

  2. After np.savez("data.npz", x=x, y=y) and loaded = np.load("data.npz"), what does loaded.files show?

    Show answer

    A โ€” The names x and y โ€” The loaded npz object lists the keyword names you saved, and you read each array with loaded["x"] or loaded["y"].

  3. What does np.savez_compressed trade off compared with np.savez?

    Show answer

    A โ€” A smaller file for a bit more CPU time โ€” Compression shrinks the file if the data compresses well, at the cost of extra CPU time.

  4. For a simple numeric matrix as text, which pair of calls does the lesson show?

    Show answer

    A โ€” np.savetxt and np.loadtxt โ€” savetxt and loadtxt work with CSV-like text, for example with delimiter set to a comma. For best speed and exact dtype preservation, prefer .npy and .npz.

Next

Continue to: Conditional Logic, Sorting & Set Logic to express if-style logic as array operations and use sort, unique, and set functions.

๐Ÿงช Try It Yourself

Exercise 1 โ€“ Save a Single Array

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 โ€“ Save Multiple Arrays with savez

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 โ€“ Round-trip Through a Text File

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did