NumPy provides a variety of file manipulation functions for accessing the contents of arrays. Files that hold array data can be in binary or text format. Files in binary format are divided into NumPy-specific formatted binary types and unformatted types.
Files in numpy format can be saved as files with a suffix (.npy/.npz) format
1. tofile() and fromfile()
- tofile() writes the data in the array to a file in binary format.
- The data output by tofile() does not preserve information such as the shape of the array and the type of the elements.
- The fromfile() function reads back data requiring the user to specify the element type and make appropriate changes to the shape of the array
import numpy as np # Randomly generate 12 numbers and convert them with one dimension into a 3*4 matrix form a = (12) print("One-dimensional array:",a) = 3,4 print("3*4 matrix:",a) # Write the data in the array to a file in binary format ('') # fromfile need to specify the data format when reading numpy files, and the original format is not saved. b1 = ('', dtype=) # Reading data by float b2 = ('', dtype=) # Read data by int b3 = ('', dtype=np.int32) # Read data according to int32 print('floatspecificationb1:{},\nintspecificationb2:{},\nint32specificationb3:{}'.format(b1,b2,b3)) = 3,4 print('b3:',b3)
2. save() and load(), savez()
- NumPy-specific binary formats hold data, and they automatically process information such as element type and shape
- If you want to save multiple arrays to a single file, you can use savez()
- The first argument to savez() is the filename, and the subsequent arguments are all arrays to be saved, which can also be named using the keyword argument
- Arrays passed with non-keyword arguments are automatically named arr_0, arr_1, ....
- The output of savez() is a compressed file with the extension npz, where each file is an npy file saved by save() with the same filename as the array name
- load() automatically recognizes npz files and returns a dictionary-like object that can be used as a key to retrieve the contents of an array.
import numpy as np a = (12) = 3,4 # Store data as npy/npz ('', a) ('', a) c = ('') print('save-load:',c) # Store multiple arrays b1 = ([[6, 66, 666],[888, 88,8]]) b2 = (0, 1.0, 0.1) c2 = (b2) ('', b1,b2,sin_arry = c) c3 = ('') # npz file is a compressed file print(c3) print("arraysb1:{}\narraysb2:{}\narrayssin_arry:{}".format(c3['arr_0'],c3['arr_1'],c3['sin_arry']))
3. savetxt() and loadtxt()
- Reads and writes text files of 1 and 2 dimensional arrays.
- They can be used to read and write text files in CSV format
With this way to store the data to facilitate deep learning, save the training set, validation set, test set, including their labels, with this way to store up, to what load what, the number of files is greatly reduced, will not go around to change the file name. Sort of get to another good way to store data
Reference:/wushaogui/p/
/dmir/p/
to this article on the use of python numpy access file case tutorial article is introduced to this, more related python numpy content please search my previous posts or continue to browse the following related articles hope that you will support me more!