A single NumPy array often needs to survive beyond the Python process that created it. The NPY format keeps that array in a NumPy-native binary file so later Python code can restore the values without rebuilding dtype and shape metadata by hand.

The np.save() function writes one array to a .npy file, and np.load() returns the saved data as an ndarray. Passing allow_pickle=False keeps numeric arrays on the non-pickle path and avoids loading Python objects from untrusted files.

NPY is a one-array file format. Use NPZ when multiple named arrays must travel together, CSV when another tool needs text, and memory mapping when a large NPY file should be sliced without reading the whole array into RAM.

Steps to save and load a NumPy array with NPY:

  1. Create a script that writes a numeric array to an NPY file.
    array-save-load-npy.py
    import numpy as np
    from pathlib import Path
     
    path = Path("calibration-readings.npy")
    readings = np.array(
        [[18.25, 18.50, 18.75], [19.00, 19.25, 19.50]],
        dtype=np.float32,
    )
     
    np.save(path, readings, allow_pickle=False)
    loaded = np.load(path, allow_pickle=False)
     
    print("saved file:", path)
    print("loaded array:")
    print(loaded)
    print("shape:", loaded.shape)
    print("dtype:", loaded.dtype)
    print("matches original:", np.array_equal(readings, loaded))

    allow_pickle=False works for numeric dtypes. Object arrays require pickling and should only come from trusted data.

  2. Run the script and verify the loaded array.
    $ python3 array-save-load-npy.py
    saved file: calibration-readings.npy
    loaded array:
    [[18.25 18.5  18.75]
     [19.   19.25 19.5 ]]
    shape: (2, 3)
    dtype: float32
    matches original: True

    The loaded array keeps the original values, shape, and float32 dtype.

  3. Remove the sample NPY file after testing.
    $ rm calibration-readings.npy

    Keep the file when it is the checkpoint another script should read later. Use a project data path instead of the current directory for long-lived arrays.