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:
load()
andsave()
functions are the two main functions for reading and writing array data to files. By default, arrays are saved in an uncompressed raw binary format with the .npy extension.savez()
function is used to write multiple arrays into a file. By default, arrays are saved in an uncompressed raw binary format with the .npz extension.loadtxt()
andsavetxt()
functions handle normal text files (.txt, etc.)
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:
file: The file to save, with the .npy extension. If the file path does not end with the .npy extension, it will be automatically added.
arr: The array to save.
allow_pickle: Optional, boolean, allows using Python pickles to save object arrays. Python's pickle is used for serializing and deserializing objects before saving to or reading from disk files.
fix_imports: Optional, to facilitate reading data saved by Python 3 in Python 2.
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:
file: The file to save, with the .npz extension. If the file path does not end with the .npz extension, it will be automatically added.
args: The arrays to save. Non-keyword arguments will be automatically named arr_0**, **arr_1, etc.
kwds: Keyword names for the arrays to save.
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.]]