NumPy Tutorial NumPy Statistics NumPy References

NumPy - load() function



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

Syntax

numpy.save(file, mmap_mode=None)

Parameters

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.

Example:

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
np.save("test", arr)

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

#displaying the content of y
print(y)

The output of the above code will be:

[10 20 30 40 50 60]

Example:

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.save(MyFile, np.array([10, 20]))
np.save(MyFile, np.array([10, 30]))
MyFile.close()

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

#displaying the content of x and y
print(x)
print(y)

The output of the above code will be:

[10 20]
[10 30]

❮ NumPy - Functions