Load ndarrays from files

The numpy load() function loads back the contents from a file into ndarrays. The load() function reads ndarrays both from .npy or .npz files.

Read Write Files Updated 23 Aug 2024 3 min read

Overview

  • The numpy.load() function loads the contents of .npy and .npz files into ndarrays.
  • While loading the .npy file which contains the serialized Python objects, the parameter allow_pickle to the load() function should have the value True. The NumPy uses the pickling mechanism of Python to read and write ndarrays of Python objects. Sending the allow_pickle parameter as False will raise a ValueError stating ValueError: Object arrays cannot be loaded when allow_pickle=False.
  • When the ndarray to be loaded is huge, it could occupy significant amount of RAM.To avoid such Out of Memory scenarios the memory mapping mechanism of NumPy can be used. The load() function utilizes the memory mapping feature of Numpy through its mmap_mode parameter. When compared to loading the whole file into the RAM, the memory mapping uses very little footprint as per the loaded portion of the array.
  • For writing to files NumPy offers multiple variants of functions like save(), savez() and savez_compressed(). The load() function handles the output of all these functions and loads them into one or more NumPy arrays.

Example 1: Load an ndarray of integers from a .npy file

python
# Example Python program that loads an ndarray of integers

            # from a .npy file

            import numpy as np

# Load ndarray from file

            array = np.load("array_data.npy")

            print(array)

Output

Output
[[10 11 12]

 [13 14 15]

 [10 11 12]]

Example 2: Load Python objects from a file into an ndarray

python
# Example Python program that loads an ndarray of Python objects from a

            # .npy file

            import numpy as np

# Have to import the Python class for unpickling the Python objects

            # (Defined locally for demonstration)

            class Point3D:

                def __init__(self, x, y, z):

                    self.myX = x

                    self.myY = y

                    self.myZ = z

    def __repr__(self):

                    strRep = "X:%d Y:%d Z:%d"%(self.myX, self.myY, self.myZ)

                    return strRep

            # Load objects from the .npy file

            array = np.load("PythonObjects.npy", allow_pickle = True)

# Print the ndarray

            for elem in array:

                print(elem)

Output

Output
[[X:0 Y:0 Z:0 X:0 Y:0 Z:1 X:0 Y:0 Z:2 X:0 Y:0 Z:3 X:0 Y:0 Z:4 X:0 Y:0 Z:5

  X:0 Y:0 Z:6 X:0 Y:0 Z:7 X:0 Y:0 Z:8 X:0 Y:0 Z:9]

 [X:0 Y:1 Z:0 X:0 Y:1 Z:1 X:0 Y:1 Z:2 X:0 Y:1 Z:3 X:0 Y:1 Z:4 X:0 Y:1 Z:5

  X:0 Y:1 Z:6 X:0 Y:1 Z:7 X:0 Y:1 Z:8 X:0 Y:1 Z:9]

.

.

.

 [X:9 Y:9 Z:0 X:9 Y:9 Z:1 X:9 Y:9 Z:2 X:9 Y:9 Z:3 X:9 Y:9 Z:4 X:9 Y:9 Z:5

  X:9 Y:9 Z:6 X:9 Y:9 Z:7 X:9 Y:9 Z:8 X:9 Y:9 Z:9]]

Example 3 - Loading multiple ndarrays from a .npz file - compressed form

python
# Example Python program that loads an ndarray

            # from a .npz file - compressed format.

            import numpy as np

# Load from .npz file

            arrays = np.load("multi_array_uncompressed.npz")

            print(type(arrays))

print("Array1 loaded from file:")

            print(arrays["array1"])

print("Array2 loaded from file:")

            print(arrays["array2"])

arrays.close()

Output

Output
<class 'numpy.lib.npyio.NpzFile'>

Array1 loaded from file:

[[0 1]

 [1 1]]

Array2 loaded from file:

[[[0 0 0]

  [0 0 0]

  [0 0 0]]

 [[0 0 0]

  [0 1 2]

  [0 2 4]]

 [[0 0 0]

  [0 2 4]

  [0 4 8]]]