Easy Tutorial
❮ Numpy Terating Over Array Numpy Array From Existing Data ❯

NumPy IO

NumPy can read and write text data or binary data on disk.

NumPy introduces a simple file format for the ndarray object: npy.

The npy file is used to store the data, shape, dtype, and other information necessary to reconstruct the ndarray.

Commonly used IO functions include:

numpy.save()

The numpy.save() function saves an array to a file with the .npy extension.

numpy.save(file, arr, allow_pickle=True, fix_imports=True)

Parameter Description:

Example

import numpy as np

a = np.array([1, 2, 3, 4, 5])

# Save to outfile.npy
np.save('outfile.npy', a)

# Save to outfile2.npy, if the file path does not end with the .npy extension, it will be automatically added
np.save('outfile2', a)

We can view the file content:

$ cat outfile.npy
?NUMPYv{'descr': '<i8', 'fortran_order': False, 'shape': (5,), }
$ cat outfile2.npy
?NUMPYv{'descr': '<i8', 'fortran_order': False, 'shape': (5,), }

The files appear garbled because they are in NumPy's proprietary binary format.

We can use the load() function to read the data and display it normally:

Example

import numpy as np

b = np.load('outfile.npy')
print(b)

Output:

[1 2 3 4 5]

np.savez

The numpy.savez() function saves multiple arrays into a file with the .npz extension.

numpy.savez(file, *args, **kwds)

Parameter Description:

Example

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.arange(0, 1.0, 0.1)
c = np.sin(b)
# c is named with the keyword argument sin_array
np.savez("tutorialpro.npz", a, b, sin_array=c)
r = np.load("tutorialpro.npz")
print(r.files)  # View the names of the arrays
print(r["arr_0"])  # Array a
print(r["arr_1"])  # Array b
print(r["sin_array"])  # Array c

Output:

['sin_array', 'arr_0', 'arr_1']
[[1 2 3]
 [4 5 6]]
[0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
[0.         0.09983342 0.19866933 0.29552021 0.38941834 0.47942554
 0.56464247 0.64421769 0.71735609 0.78332691]

savetxt()

The savetxt() function stores data in a simple text file format, and the corresponding loadtxt() function is used to retrieve the data.

np.loadtxt(FILENAME, dtype=int, delimiter=' ')
np.savetxt(FILENAME, a, fmt="%d", delimiter=",")

The delimiter parameter can specify various delimiters, converter functions for specific columns, the number of rows to skip, and more.

Example

import numpy as np 

a = np.array([1,2,3,4,5]) 
np.savetxt('out.txt',a) 
b = np.loadtxt('out.txt')  

print(b)

The output result is:

[1. 2. 3. 4. 5.]

Using the delimiter parameter:

Example

import numpy as np 

a=np.arange(0,10,0.5).reshape(4,-1)
np.savetxt("out.txt",a,fmt="%d",delimiter=",") # Changed to save as integers, comma-separated
b = np.loadtxt("out.txt",delimiter=",") # Specify comma-separated when loading
print(b)

The output result is:

[[0. 0. 1. 1. 2.]
 [2. 3. 3. 4. 4.]
 [5. 5. 6. 6. 7.]
 [7. 8. 8. 9. 9.]]
❮ Numpy Terating Over Array Numpy Array From Existing Data ❯