×

NumPy I/O Functions

The NumPy package provides functions for reading and writing the arrays as text files as well as in a simple numpy binary format(.npy).

loadtxt() and savetxt():

The loadtxt() function of NumPy loads data from a text file. Each row in the text file must have the same number of values(elements).

Syntax:

loadtxt(fname)

The fname parameter is the file or filename to read. If the filename extension is .gz or .bz2, the file is first decompressed and then read.

The savetxt() function of NumPy saves an array to a text file.

Syntax:

savetxt(fname, X)

The fname parameter is the filename to which the data is saved. If the filename ends .gz, the file is automatically saved in compressed gzip format. The X parameter is the data to be saved to a text file.

import numpy as np
input_array = np.array([[2, 3, 5], [7, 11, 13], [17, 19, 23]])
np.savetxt("input.txt", input_array)
txt_array = np.loadtxt("input.txt")

print(txt_array)
#Output:
[[ 2.  3.  5.]
 [ 7. 11. 13.]
 [17. 19. 23.]]

If the text file does not have the same number of values(elements) in each row:

Test Condition
txt_array = np.loadtxt("C:/Users/risha/Documents/test_condition.txt")
print(txt_array)

#Output:
ValueError: Wrong number of columns at line 2

load() and save():

The load() function of loads data from a numpy binary file format(.npy extension).

Syntax:

load(file)

The file parameter is the File or filename to which the data is saved.

The Numpy save() function stores the input array in a numpy binary file format(.npy extension).

Syntax:

save(file, arr)

The file parameter is the File or filename to which the data is saved. The arr parameter is the array data to be saved.

input_array = np.arange(1, 25).reshape(4, 6)
np.save("input", input_array)

load_array = np.load("input.npy")
print(load_array)

#Output:
[[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]
 [13 14 15 16 17 18]
 [19 20 21 22 23 24]]

savez():

The savez() function saves several arrays into a single file (in uncompressed .npz format).

Syntax:

savez(file, *args, **kwds)

The file parameter is either the filename (string) or an open file (file-like object) where the data will be saved.  The args and kwds parameters are the arrays to save to the file. Arrays specified as args will be named “arr_0”, “arr_1”, and so on. Using the kwds parameter, each array will be saved to the output file with its corresponding keyword name.

array1 = np.arange(1, 10).reshape(3, 3)
array2 = np.eye(3)
array3 = np.linspace(3, 18, 9).reshape(3, 3)
#saving arrays with *args parameters
np.savez("Arrays", array1, array2, array3)
arg_data = np.load("Arrays.npz")
print(arg_data.files)

#Output:
['arr_0', 'arr_1', 'arr_2']

print(arg_data['arr_0'])

#Output:
[[1 2 3]
 [4 5 6]
 [7 8 9]]

#saving arrays with keyword parameters
np.savez("arrays", array1 = array1, array2 = array2, array3 = array3)
kwd_data = np.load("arrays.npz")
print(kwd_data.files)

#Output:
['array1', 'array2', 'array3']

print(kwd_data['array3'])

#Output:
[[ 3.     4.875  6.75 ]
 [ 8.625 10.5   12.375]
 [14.25  16.125 18.   ]]

savez_compressed():

The savez_compressed saves multiple arrays into a single file in compressed .npz format.

Syntax:

savez_compressed(file, *args, **kwds)
array1 = np.arange(1, 10).reshape(3, 3)
array2 = np.eye(3)
array3 = np.linspace(3, 18, 9).reshape(3, 3)
#saving arrays with *args parameters
np.savez_compressed("Arrays", array1, array2, array3)
npz_data = np.load("Arrays.npz")
print(npz_data.files)

#Output:
['arr_0', 'arr_1', 'arr_2']

print(npz_data['arr_1'])

#Output:
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]