NumPy Tutorial NumPy Statistics NumPy References

NumPy - Input and output



The NumPy objects can be saved to a disk file and loaded from it. Below mentioned are most commonly used functions to achieve this:

FunctionDescription
save() Save an array to a binary file in NumPy .npy format.
load() Load arrays from .npy file.
savetxt() Save an array to a text file.
loadtxt() Load data from a text file.

Lets discuss these functions in detail:

numpy.save() function

The NumPy save() function is used to save an array to a binary file in NumPy .npy format.

Syntax

numpy.save(file, arr)

Parameters

file Required. Specify file or filename to which the data is saved. If file is a file-object, then the filename is unchanged. If file is a string or Path, a .npy extension will be appended to the filename if it does not already have one.
arr Required. Specify array data to be saved.

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.

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.savetxt() function

The NumPy savetxt() function is used to save an array to a text file.

Syntax

numpy.savetxt(fname, X)

Parameters

fname Required. Specify the filename to which the data is saved. If the filename ends in .gz, the file is automatically saved in compressed gzip format.
X Required. Specify data to be saved in the file (1D or 2D array_like).

numpy.loadtxt() function

The NumPy loadtxt() function is used to load data from a text file. Each row in the text file must have the same number of values.

Syntax

numpy.loadtxt(fname, dtype=<class 'float'>)

Parameters

fname Required. Specify File, filename, or generator to read. If the filename extension is .gz or .bz2, the file is first decompressed. Note that generators should return byte strings.
dtype Optional. Specify data-type of the resulting array. Default: float.

Example:

In the example below, array arr is saved into a text file called test.out. Further, the loadtxt() 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 text file - test.out
np.savetxt("test.out", arr)

#loading array from test.out
y = np.loadtxt("test.out")

#displaying the content of y
print(y)

The output of the above code will be:

[10. 20. 30. 40. 50. 60.]

Example:

The example below describes how to save multiple numpy arrays in a file and load the saved arrays from it. Please note that, each array must have same number of elements.

import numpy as np

x = y = z = np.arange(0.0,5.0,1.0)
np.savetxt("demo.txt", (x, y, z))

#loading array from demo.txt
NewArr = np.loadtxt("demo.txt")

#displaying the result
print(NewArr)

The output of the above code will be:

[[0. 1. 2. 3. 4.]
 [0. 1. 2. 3. 4.]
 [0. 1. 2. 3. 4.]]