NumPy IO
NumPy can read and write text data or binary data on disk.
NumPy introduces a simple file format for ndarray objects:npy。
The npy file is used to store the data, shape, dtype, and other information required to reconstruct the ndarray.
Commonly used IO functions include:
- The load() and save() 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 in files with the .npy extension.
- The savez() function is used to write multiple arrays to a file. By default, arrays are saved in an uncompressed raw binary format in files with the .npz extension.
- The loadtxt() and savetxt() functions handle normal text files (such as .txt).
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:
- filefile: The file to be saved, with the .npy extension. If the file path does not end with the .npy extension, the extension will be added automatically.
- arrarr: The array to be saved.
- allow_pickleallow_pickle: Optional, boolean. Allows object arrays to be saved using Python pickles. Pickle in Python is used to serialize and deserialize objects before saving to or reading from a disk file.
- fix_importsfix_imports: Optional, for convenience when reading data saved by Python3 in Python2.
Example
We can view the file contents:
$ cat outfile.npy
?NUMPYv{'descr': '<i8', 'fortran_order': False, 'shape': (5,), }
$ cat outfile2.npy
?NUMPYv{'descr': '<i8', 'fortran_order': False, 'shape': (5,), }
It can be seen that the file is garbled, because it is data in Numpy's specific binary format.
We can use the load() function to read the data and it will display normally:
Example
The output is:
[1 2 3 4 5]
np.savez
The numpy.savez() function saves multiple arrays to a file with the .npz extension.
numpy.savez(file, *args, **kwds)
Parameter description:
- filefile: The file to be saved, with the extension.npz, if the file path does not end with the extension.npz, the extension will be added automatically.
- argsargs: The arrays to be saved. You can use keyword arguments to name the arrays. Arrays passed as non-keyword arguments will be automatically namedarr_0, arr_1, … 。
- kwdskwds: Name the arrays to be saved using keyword arguments.
Example
The output is:
['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 so on.
Example
The output is:
[1. 2. 3. 4. 5.]
Using the delimiter parameter:
Example
The output is:
[[0. 0. 1. 1. 2.] [2. 3. 3. 4. 4.] [5. 5. 6. 6. 7.] [7. 8. 8. 9. 9.]]Other Extensions