NumPy Tutorial NumPy Statistics NumPy References

NumPy - load() function

The NumPy load() function is used to load arrays from .npy files.

Syntax, mmap_mode=None)


file Required. Specify the file to read. File-like objects must support the seek() and read() methods.
mmap_mode Optional. It can take value from {None, 'r+', 'r', 'w+', 'c'}. Default is None. If not None, then memory-map the file, using the given mode. A memory-mapped array is kept on disk. However, it can be accessed and sliced like any ndarray. Memory mapping is especially useful for accessing small fragments of large files without reading the entire file into memory.

Return Value

Returns the data read from the .npy file.


In the example below, array arr is saved into a new binary file called test.npy. Further, the load() function is used to load the saved array from the file and print it

import numpy as np

arr = np.array([10, 20, 30, 40, 50, 60])

#saving arr in binary file - test.npy"test", arr)

#loading array from test.npy
y = np.load("test.npy")

#displaying the content of y

The output of the above code will be:

[10 20 30 40 50 60]


Lets assume that we have a file called demo.npy. The example below describes how to save numpy arrays in it and load the saved arrays from it.

import numpy as np

#open the file in write mode 
#to save numpy arrays
MyFile = open("demo.npy", "wb"), np.array([10, 20])), np.array([10, 30]))

#open the file to read contant
MyFile = open("demo.npy", "rb")
x = np.load(MyFile)
y = np.load(MyFile)

#displaying the content of x and y

The output of the above code will be:

[10 20]
[10 30]

❮ NumPy - Functions